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();
}
You're splitting
name1
around spaces. You can try to splitname1
around any non-word character:Method String.split accepts regex as argument. To quote from the docs:
name1.split(" ");
splits string around single space and returns array:[1298, Anthony.ef, 1298, Anthony.ef]
names.split("\\W+");
splits string around any non-word character (comma, dot, dash, etc.) and returns array:[1298, Anthony, ef, 1298, Anthony, ef]
As you can see in this case it was able to splitAnthony.ef
into separate strings.UPDATE: If you want to preserve word's order in the original string you might want to use
LinkedHashSet
instead ofHashSet
. For example:Check out this question: Is there an insertion order preserving Set that also implements List?