Get substring after backslash in a string

HarshTimes
Tera Guru

hi
I have a string that contains a backslash.I want the substring after backslash. How can i get it?I tried substring function but its not working.PLease help me out.
example if the string is "first\last" I want to store 'last' in a variable.

14 REPLIES 14

You have to understand something called escaping.


var str = 'first\last';



in this, \ is used to escape things like

' " \ /
and as there is nothing to be escaped it becomes just 'firstlast '

because you want to have a \ in between first and last, you need to initialize the string to 'first\\last' (escaping '\') and then try my code. Let me know if it works.

Oh, noes, but the SCCM doesn't send it that way - SCCM when it sends, it will be a string already, so you don't need to escape it again.

To read more on it, Google escaping strings.

example:


var str = "first\\last";
var lst = str.substr(str.indexOf('\\')+1);
gs.log(lst) ;



yup, that I understand about the escape character and the other stuff, thats why I said earlier its looking simple but its not.
the data coming from sccm is as "domain\user" in staging table for a user field and I cant make it "domain\\user".the sccm is not a manual process.I want only 'user' to populate in the target table.So i need to add transform script to take the substring 'user' from "domain\user".
Did u got the query??
the string is "domain\user". it cannot be changed or modified.
and i want the substring 'user'.


I did get the query.

When you log SCCM thing are you getting 'domain\user'? or 'domainuser'?

If you are getting 'domain\user' it's already being escaped.


From sccm i get 'domain\user'. But i need 'user' only


Jace Benson
Mega Sage

Another way that works is using the split method;


//say str contains the string
var str = 'first\\last';//returns 'first\last'
var arr = str.split('\\');
gs.log(arr[1]);//returns last