+ Start a Discussion
Michael MMichael M 

Help with parsing string

Hello, I was wondering if someone can help me write the apex to parse a string like this (changed the details of it, but the concept is still what I need):

https://abc-park-store.facilities.facilities.org/contact-form-block/

From that string, I only want everything between "https://" and ".facilities...". Also, I want to replace the "-" with spaces, and capitalize each word. So from the above string, I would want it to say "Abc Park Store". How can I parse the string as such? Thank you.
Best Answer chosen by Michael M
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

Verified in my org. The following code gives the output you are looking for
 
String str = 'https://abc-park-store.facilities.facilities.org/contact-form-block/';
String str2 = str.substringAfter('//').substringBefore('.facilities'); 
System.debug('split string is' + str.substringAfter('//').substringBefore('.facilities')); 
System.debug('string' + str2.substring(0,1).toUpperCase()+str2.substring(1,3)+'-'+str2.substring(4,5).toUpperCase()+str2.substring(5,8)+'-'+str2.substring(9,10).toUpperCase()+str2.substring(10,14));

I referred to the methods available in the string class

Let me know if it helps

Anudeep

All Answers

David Zhu 🔥David Zhu 🔥
You can use String.split('.') to get string array, take the first string.  then split('//'), get the second one from the array.
Michael MMichael M
Thanks David
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

Verified in my org. The following code gives the output you are looking for
 
String str = 'https://abc-park-store.facilities.facilities.org/contact-form-block/';
String str2 = str.substringAfter('//').substringBefore('.facilities'); 
System.debug('split string is' + str.substringAfter('//').substringBefore('.facilities')); 
System.debug('string' + str2.substring(0,1).toUpperCase()+str2.substring(1,3)+'-'+str2.substring(4,5).toUpperCase()+str2.substring(5,8)+'-'+str2.substring(9,10).toUpperCase()+str2.substring(10,14));

I referred to the methods available in the string class

Let me know if it helps

Anudeep
This was selected as the best answer
Michael MMichael M
Excellent, thank you!