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
DJP1SDJP1S 

Splitting '(' and ')' in a string get middle middle characters

Hi, I have a picklist with values like "Boo Foo (2)[0]"

 

I want to grab the 2 and use it later in a DML statement. However, when I write my code like this:

 

String s = t.Prospect_Phase_Change__c;
                    system.debug('%%% s: ' + s);
                    s.split('(', 1);
                    system.debug('$$$ s: ' + s);

 I get the error "Invalid regex: Unclosed group near index 1 ( ^"

 

I was planning to split it to the right of the '(' and the left of the ')' and then write that value elsewhere.

Best Answer chosen by Admin (Salesforce Developers) 
Jia HuJia Hu
try this,
String s = 'Boo Foo (2)[0]';

system.debug(' -------- ' + s.substring( s.indexOf( '(' ) + 1, s.indexOf( ')' ) ) );

All Answers

Jia HuJia Hu
try this,
String s = 'Boo Foo (2)[0]';

system.debug(' -------- ' + s.substring( s.indexOf( '(' ) + 1, s.indexOf( ')' ) ) );
This was selected as the best answer
~Onkar~Onkar

Hi

 

Try this may be it will solve your use.

 


String s = t.Prospect_Phase_Change__c; system.debug('%%% s: ' + s); String[] S1 = s.split('\\(', 1); system.debug('$$$ s: ' + S![0]);
DJP1SDJP1S

Thanks, here's what I did:

 

s = s.substring( s.indexOf( '(' ) + 1, s.indexOf( ')' ) );