Remove repetitive string from sentence in java

156 views Asked by At

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();
}
1

There are 1 answers

9
default locale On BEST ANSWER

You're splitting name1 around spaces. You can try to split name1 around any non-word character:

names.split("\\W+");

Method String.split accepts regex as argument. To quote from the docs:

Splits this string around matches of the given regular expression.

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 split Anthony.ef into separate strings.

UPDATE: If you want to preserve word's order in the original string you might want to use LinkedHashSet instead of HashSet. For example:

public static void removeString(){

    String name1 = "1298 Anthony.ef 1298 Anthony.ef";

    String[] strArr = name1.split("\\W+");
    Set<String> set = new LinkedHashSet<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();
}

Check out this question: Is there an insertion order preserving Set that also implements List?