function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nelson-BenavidesNelson-Benavides 

Extract specific text from a String

Hi there,

I already have all the code working, but at the end I don't need to insert all the content of the String variable.

Suppose you have a String variable called: temp --- and this variable contains the following text:

/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*
abc abc abc
defghi
jklmnopq 1234 56789

"Start"
More text More text More text More text More text More text More text More text More text More text More text
"End"

1234567890123456 Text Text Text Text Text Text Text Text
More Text Text Text Text Text Text
/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*

I only need to "extract" or "insert" the text that is between "Start" and "End" instead of inserting everything.

How do I do this? What method(s) and/or Functions could I use?

Thanks in advance.

--Nelson.
Best Answer chosen by Nelson-Benavides
Vamsi KrishnaVamsi Krishna
you can use the string method INDEXOF to get the indexes of Start and End and then use the function Substring to extract the string between the 2 indexes..

temp.Substring(temp.indexof('start')+4,temp.indexof('end')+3);

All Answers

kevin lamkevin lam
Try temp.substringAfter('Start').substringBefore('End')
Vamsi KrishnaVamsi Krishna
you can use the string method INDEXOF to get the indexes of Start and End and then use the function Substring to extract the string between the 2 indexes..

temp.Substring(temp.indexof('start')+4,temp.indexof('end')+3);
This was selected as the best answer
Nelson-BenavidesNelson-Benavides
Thank you Kevin and Vamsi.

I ended up implementing the one with INDEXOF and it worked out perfectly.

Thanks again!
Daniela MalagonDaniela Malagon

Kevin, thanks for your support, even 5 years later it helped to me to obtain a specific value from an URL:
hope this helps someone :)

I wanted to extract from this String: '/practice/*/prescriber'
I did thisString foo = req.requestURI.substringAfter('/practice/').substringBefore('/prescribers);

and it worked perfectly for me :D

 

Jiwesh ChauhanJiwesh Chauhan
It can be use like extract accountId from URI:- https://keysighttechologies-dev-ed.my.salesforce.com/services/apexrest/Accounts/0015g000016K4KBAA0/contacts
Id accountId = ((request.requestURI.subStringAfter('Accounts/')).subStringBefore('/contacts'));
accountId will have 0015g000016K4KBAA0