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
SFDC New learnerSFDC New learner 

how to remove multiple occurences in a given string?

Hi All,

How to remove muliple occurences from a string?say for example if we give input as 'MISSISSIPPI' .I need to get output as 'MISP'.

can anyone please help on this.

Thanks,
Sirisha
Best Answer chosen by SFDC New learner
KapilCKapilC
Hi Sirisha

Please find the code attached.
 
string inputStr = 'MISSISSIPPI';
set<integer> uniqueSet = new set<integer>();
uniqueSet.addAll(inputStr.getChars());
system.debug('::::::'+string.fromCharArray(new list<integer>(uniqueSet)));

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)

All Answers

DeveloperSudDeveloperSud
Hi,
A quick solution .Let me know if this helps.
 
string testString='MISSISSIPPI';
list<String> testList=testString.split('');
set<string> uniqueString= new set<string>();
uniqueString.addAll(testList);
String neededString;
for(String s:uniqueString){
    if(neededString !=null){
    neededString+=String.valueOf(s);
    }else{
        neededString=String.valueOf(s);
    }
}
system.debug('neededString  '+neededString);

 
KapilCKapilC
Hi Sirisha

Please find the code attached.
 
string inputStr = 'MISSISSIPPI';
set<integer> uniqueSet = new set<integer>();
uniqueSet.addAll(inputStr.getChars());
system.debug('::::::'+string.fromCharArray(new list<integer>(uniqueSet)));

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
This was selected as the best answer
SFDC New learnerSFDC New learner
Thank u Kapil.