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
Drew Gehman 18Drew Gehman 18 

Finding Parentheses using a string class

I am trying to return the string found between parentheses in a field.  Something like this:
String s1 = 'My favorite color is xYellowy';
String s2 = s1.substringBetween('x','y');
System.assertEquals('Yellow', s2);
If I substitute square brackets for x and y everything works well. When using traditional parentheses, I thought I would need to escape the parentheses, like this:
String s1 = '(Yellow)';
String s2 = s1.substringBetween('\\(','\\)');
System.assertEquals( 'Yellow', s2);
But that didn't work either.  I bet I am 90% there, but I am missing something, 

 
Best Answer chosen by Drew Gehman 18
AG_SFDCAG_SFDC
Hello,

Try this:
 
String s1 = '(Yellow)';
String s2 = s1.substringBetween('(',')');
System.assertEquals( 'Yellow', s2);

Let me know if it helped you!

Thanks,
AG
 

All Answers

AG_SFDCAG_SFDC
Hello,

Try this:
 
String s1 = '(Yellow)';
String s2 = s1.substringBetween('(',')');
System.assertEquals( 'Yellow', s2);

Let me know if it helped you!

Thanks,
AG
 
This was selected as the best answer
Temoc MunozTemoc Munoz
Hi Drew.

You don't need to escape the parentheses.

Just try:
String s2 = s1.substringBetween('(',')');


 
Drew Gehman 18Drew Gehman 18
As God as my witness, I tried that as my first attempt. And my second, and my third.....I must have had something else laying around that spit my error. Of course I try it now and 
String s2 = s1.substringBetween('(',')');

works perfectly.

C'est la vie. Thanks all.