How to split or trim or slice a String

ursnani
Giga Guru

Hi All,

 

I am trying to split/trim/slice the following string "aaaaaaa-test1:\ritmtest123_testgrow_test\testtest"  and I am trying to get value "test"(3rd value) from "ritmtest123_testgrow_test".

I Tried to Split with "\" but dint work so tried Trim which didnt work either.

Can someone please help with extracting the above mentioned string from the given string.

 

Thanks

1 ACCEPTED SOLUTION

ursnani
Giga Guru

Hi All,

 

Thanks for all your response, but the path which user provide will not be consistent, so I used the indeOf() to search for the word to achieve what I need.

 

Again Thanks for all your responses.

 

Thank you.

View solution in original post

7 REPLIES 7

Chander Bhusha1
Tera Guru

Hi ursnani,

The easiest way would be to split the string by('\') then third element would be your sting.

use :

var str = 'aaaaaaa-test1:\ritmtest123_testgrow_test\testtest';

var b = str.split('\');

var c = b[2];

gs.print(c);

c would retunr you the testtest

 

Mark helpful and correct if it helps.

Thanks,

CB

Namrata Khabale
Giga Guru

Hey ursnani,

try like this:

const originalString = "How are you?";
 //enter your string here
// Split string by whitespace character
const splitString = originalString.split(" ");
var s=spliString[2];
gs.print(s);

also refer the thread:
https://community.servicenow.com/community?id=community_question&sys_id=99ef87a5dbdcdbc01dcaf3231f96...



Mark Correct and Helpful if you find my response worthy!!!

 

Best Regards,

Namrata.

 

Chander Bhusha1
Tera Guru

Hi ursnani,

The above string contain escape characters so it will the value of those characters in java script stings.so to 

for example : if it contains \n , or \t or \r it will repalce with the special charactes so you have to check if the string has those characters then do substring to get the values.

 

For the above example the code should be :

var str = "aaaaaaa-test1:\ritmtest123_testgrow_test\testtest"; // Enter sting

var patt1 = /\t/;  //Checking \t
 var result = str.search(patt1); //return the position of \t in the sting

//In above the position is 39 so we have used that position and till the last lenght

var sub = str.substring(result,str.length);
 
var final_strng = 't'+ sub; //Gives you the third element value which is testtest

Escape characters document

https://www.w3schools.com/js/js_strings.asp

 

 

Mark helpful and correct if it helps

Thanks,

CB

MrMuhammad
Giga Sage

Hi @ursnani,

If you want to extract "test"(3rd value) from "ritmtest123_testgrow_test".

try this in background script for quick output 

var str = "aaaaaaa-test1:\ritmtest123_testgrow_test\testtest"; // Enter sting

var patt1 = /\t/; 
var patt2 = /\r/;

var end = str.search(patt1); //return the position of \t in the sting
var start = str.search(patt2); //return the position of \r in the sting

var middleString = str.substring(start, end); // splicing the middle portion of the string

var sp = middleString.split('_'); // splitting on the basis of underscore '_' to get the third value from middle portion of stirng


gs.print(sp[2]); // it will print the desired output.

// return sp[2];

output:  

find_real_file.png

 

Please mark my response ACCEPTED & HELPFUL if it resolves your issue. 

 

Thanks & Regards,

Sharjeel

 

Regards,
Muhammad