I know how to remove duplicated characters from a String and keeping the first occurrences without regex:
String method(String s){
String result = "";
for(char c : s.toCharArray()){
result += result.contains(c+"")
? ""
: c;
}
return result;
}
// Example input: "Type unique chars!"
// Output: "Type uniqchars!"
I know how to remove duplicated characters from a String and keeping the last occurrences with regex:
String method(String s){
return s.replaceAll("(.)(?=.*\\1)", "");
}
// Example input: "Type unique chars!"
// Output: "Typnique chars!"
As for my question: Is it possible, with a regex, to remove duplicated characters from a String, but keep the first occurrences instead of the last?
As for why I'm asking: I came across this codegolf answer using the following function (based on the first example above):
String f(char[]s){String t="";for(char c:s)t+=t.contains(c+"")?"":c;return t;}
and I was wondering if this can be done shorter with a regex and String input. But even if it's longer, I'm just curious in general if it's possible to remove duplicated characters from a String with a regex, while keeping the first occurrences of each character.