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
JNicJNic 

Parsing specific string/integer

Hi all, I have a controller that it's supposed to parse Subject line of an Email and extract a certain set of stings. For example, If the Subject line is [112233-notification number 22] - [Customer Support], I want to be able to extract only the "112233" portion of the text. I use the code below to find the index of "[" & index of last "-". String extract = c.Subject.substring(c.Subject.indexOf('[')+1, c.Subject.LastIndexOf('-')); However, I received "112233-notification number 22]" as my extract string. How should I go about locating only the "112233" part? My method needs to smart enough to locate only the integer between [ & - (within the closed [ ]). Appreciate your inputs.
zachelrathzachelrath

If you only want the "112233" part, do this:

 

String extract = c.Subject.substring(c.Subject.indexOf('[')+1, c.Subject.indexOf('-'));

JNicJNic

In this example:

[112233-0099-notification number 22] - [Customer Support],

 

String extract = c.Subject.substring(c.Subject.indexOf('[')+1, c.Subject.IndexOf('-'));

 

extract returns 112233, but what about if I wanted to get 0099 or the string between the the two dashes.

 

Is there a string method that I can use to narrow my parsing down to the first [ and - combination?

zachelrathzachelrath

If you are only interested in the content of the first bracketed section (i.e. [112233-0099-notification number 22]), then you should extract that part first, then split up the extract.

 

// Split your String into the bracketed sections

List<String> bracketedSections = c.Subject.split(' - ');

 

// You now have the following:

// bracketedSections[0] = '[112233-0099-notification number 22]';

// bracketedSections[1] = '[Customer Support]';

 

// Now, remove the brackets from your sections

for (String s : bracketedSections) s = s.replace('[','').replace(']','');

 

// You now have the following:

// bracketedSections[0] = '112233-0099-notification number 22';

// bracketedSections[1] = 'Customer Support';

 

// You can now split up your substrings even further, i.e.

List<String> subsections = bracketedSections[0].split('-');

 

// subsections[0] = '112233';

// subsections[1] = '0099';

// subsections[2] = 'notification number 22';

Meehir MeneMeehir Mene
I have the same query when I want to get the result from <idval>12345</idval> as 12345  getting error:  
System.StringException: Ending position out of bounds: 0

Used the same code as explained above  String extract = c.Subject.substring(c.Subject.indexOf('>')+1, c.Subject.indexOf('<'));