I have a string like 622,645,677,622,689,673,622 I want to remove the duplicates from the above string. i have tried with (\d{3},)* but the duplicates were not detected. pls help me in writing expression to avoid duplicates. I have tried using collections also but they are not working.
Try:
String tmp = '622,645,677,622,689,673,622';
Set<String> tmpSet = New Set<String>(tmp.split(','));
tmp = string.join(New List<String>(tmpSet),',');
You will need to add in null checks etc...
This takes a string and splits it into a set using ',' to split (sets hold unique values) then joins it back into a string
-
Assuming tmp is not null, tmpSet will be not null, nor contain any null values. This code is perfectly fine as is. – sfdcfox Nov 21 '15 at 20:59
-
@sfdcfox - Yes, assuming the OP will use it exactly as it, for all I know tmp could be set via a VF page and could be null as the OP never said what the string was or how it was set – Eric Nov 21 '15 at 21:06