+ Start a Discussion
shweta chadha 4shweta chadha 4 

take the substring out of string in salesforce

Hello,

I am creating the html template in Salesforce and below is value i am fetching from one field that is building address. However, i just need the building number from the entire address. Below is the scenario.
“Václavské náměstí 785/28, P1 - Alfa Building”, this is the text and I want to extract the only the number i.e 785/28. But the thing is the numbers before and after the ‘/’ varies it can be more than 3 or 2 digits. Trim Left and Right work but can't seem to specify the values dynamically. 

Thanks, Shweta
BALAJI CHBALAJI CH
Hi Shweta,

I suppose you need to use Pattern and Matcher.
Please see below sample code:
string s ='“Václavské náměstí 785/28, P1 - Alfa Building”';
string[] ss = s.split('/');
string reqStr='';
for(string x : ss)
{
    pattern myPattern = pattern.compile('\\d+'); 
    matcher myMatcher = myPattern.matcher(x);
    if(reqStr != '')
        reqStr +='/';
    
    if(myMatcher.find())
        reqStr +=myMatcher.group();
}
system.debug('reqStr-'+reqStr);

Let us know if this works for you.

Best Regards,
​​​​​​​BALAJI