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
cyberdynebotcyberdynebot 

Remove text from String between characters which appear more than once

I have a String value from a Long Text field that contains Name/Email values. I need to extract only the names separated by a semi colon when there are multiple entries (no semi colon in the text if only one name) using Apex/String methods into another variable. Essentially strip out anything that begins with the pipe delimiter ' | ' and ends with '.com' (will always end in '.com').

Single value scenario: 
John Test | jtest@none.com

Expected:
John Test

Multiple values:
Scott Rogers | srogers@none.com; Mike Smith | msmith@none.com; Matt White | mwhite@none.com

Expected:
Scott Rogers; Mike Smith; Matt White

I've tried using substring/left/right but my approach may not be correct as I'm getting a null value each time and I also believe I need an array/list to grab each entry if multiple matches.

Example of test:
s1 = 'Scott Rogers | srogers@none.com; Mike Smith | msmith@none.com; Matt White | mwhite@none.com';

String s2 = s1.right(1).substringBetween(' |','.com');
Best Answer chosen by cyberdynebot
Surya GSurya G
Hi, 
String s1 = 'Scott Rogers | srogers@none.com; Mike Smith | msmith@none.com; Matt White | mwhite@none.com';
List<String> splitList = s1.split(';');
List<String> nameList = new List<String>();
for( String str : splitList) {
    nameList.add(str.substringBefore('|'));
}
system.debug(nameList);
The final output is list of names. If you want as a single string , you can convert the list back to single string with all names.
Hope this will help. 

Thanks
Surya