+ Start a Discussion
Vijay NagarathinamVijay Nagarathinam 

Delete duplicate characters in a string

Hi,

I am using a string and store some characters in the string. If the string contains any duplicate value then remove that character and store the unique characters only.

Ex String demoString = 'ADVABD';

In the above string, I want to remove the duplicate characters A and D in the string.

Let me know how to achieve this.

Thanks,
Vijay
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
 
String demoString = 'ADVABD';
String finalstr='';
Integer i=0;
for(i=0;i<demoString.length();i++){
    if(!finalstr.contains(demoString.substring(i,i+1)))
    	finalstr+=demoString.substring(i,i+1);
}
finalstr.trim();
System.debug('str:'+finalstr);

Let us know if it helps.