I am working on java code in which I want to remove repetitive words. Following code works fine to remove them if I get space in any word for example:
1298 Anthony 1298 Anthony
, it will make it like:
1298 Anthony
But for any other special character like:
1298 Anthony.ef 1298 Anthony.ef
, it will show it like:
ef. 1298 Anthony
.
My method is given below, I want to make it work for every special character, specially for : coma(,) , fullstop(.), dash(-), underscore(_). Please help me in this problem.
public static void removeString(){
String name1 = "1298 Anthony.ef 1298 Anthony.ef";
String[] strArr = name1.split(" ");
Set<String> set = new HashSet<String>(Arrays.asList(strArr));
String[] result = new String[set.size()];
set.toArray(result);
StringBuilder res = new StringBuilder();
for (int i = 0; i < result.length; i++) {
String string = result[i];
if(i==result.length-1){
res.append(string);
}
else{
res.append(string).append(" ");
}
}
System.out.println(res.toString());
String abc = res.toString();
}