+ Start a Discussion
Radhe Shyam.Radhe Shyam. 

get substring before a specific character

Hi Guys.

I am looking to get a substring before a specific character from a string .
For Example:
If Strings are

string s= 'gangadhar@SHAKTIMAAN';
String s2= 'raj@NAGRAJ';

i need s as gangadhar from first string and raj from second string.

Please guide.
 
Best Answer chosen by Radhe Shyam.
David HalesDavid Hales
Hi Radhey,
Try below code
string a = string s= 'gangadhar@SHAKTIMAAN';

String str = final_name.SubStringBefore('@');
now Str will display 'ganadhar '

Thanks,
David(1027)

All Answers

Yaroslav FilykYaroslav Filyk

is this Java?

Something like this would probably do the job

String[] parts = string.split("@");

String part1 = parts[0]; // gangadhar

String part2 = parts[1]; // NAGRAJ

Radhe Shyam.Radhe Shyam.
This is apex..
Yaroslav FilykYaroslav Filyk
then refer to https://developer.salesforce.com/forums/?id=906F00000008xyQIAQ
David HalesDavid Hales
Hi Radhe,
Try this.:
index = sec.indexOf('@');
secName = sec.mid(0,index);

Thanks and Regards,
David(1065)
David HalesDavid Hales
Hi Radhey,
Try below code
string a = string s= 'gangadhar@SHAKTIMAAN';

String str = final_name.SubStringBefore('@');
now Str will display 'ganadhar '

Thanks,
David(1027)
This was selected as the best answer
Radhe Shyam.Radhe Shyam.
Thanks David Hales for providing these solutions...both are working but last one is saving my line of code.