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
Narayana Reddy 30Narayana Reddy 30 

I have a requirement to split the string based on delimeter, compare the tokens with specific value and if it finds a match, clear that specific value . Example 1 : string 'test,krishna,shekhar, karjun' need to be split base

I have a requirement to split the string based on delimeter, compare the tokens with specific value and if it finds a match, clear that specific value or replace it with other value .
Example 1 : string 'test,krishna,shekhar, karjun' need to be split based on comma (,) and compare each value with word 'shekhar', since match is found clear the word shekhar from original string . So the updated string value will be ''test,krishna,karjun' ( Shekhar is removed including delimeter )
Can you plesae suggest the best way to achieve this rquirement ?
Bryan LeamanBryan Leaman
Hmm. Sounds more like a test question rather than a real "requirement".
You'll want to explore String.split, List.indexOf, List.remove and String.join class methods.
Narayana Reddy 30Narayana Reddy 30
Hi Bryan,
It is a real requirement. Can you please add sample code ?
Narayana Reddy 30Narayana Reddy 30
Updated the requirement for more clarity 

I have a requirement to replace specific text separated by a delimeter (delimeter can be , : ; etc). Example 1 : In string 'Sonu,krishna,Siva, pintoo' , need to replace specific token/text "Pintoo" , resultant string value will be 'Sonu, Krishna,Siva' . Example 2 : In string 'Padma;Rama;Lakshmi;Aruna;Usha', replace specific token 'Rama' , resultant string value will be 'Padma;Lakshmi;Aruna;Usha' ( Removed token Rama)
I tried it via split() method and rejoining the string etc, however looking for if there is any simplest way to get this done
kapil sharma 185kapil sharma 185
Hello Narayan, 

This can be done using the some string class methods. Here is the below solution of your problem. 

String s1 = 'shekhar,test,krishna,shekhar,karjun,shekhar';
if(s1.containsIgnoreCase('shekhar,')){
    s1=s1.replace('shekhar,','');
}
s1=s1.removeEnd(',shekhar');
system.debug(s1);


If the solution is helpul to you, Please mark it as a best answer.

Thanks 
Kapil Sharma