0

I'm having a problem getting the unique letters and digits out of an array of strings, and then returning them. I am having a formatting issue.

The given input is :([abc, 123, efg]) and is supposed to return abcefg123

however, mine returns: abc123efg

how can I fix this?

here is my method so far:

public static String getUniqueCharsAndDigits(String[] arr) {

String str = String.join(",", arr);
String myString = "";
myString = str.replaceAll("[^a-zA-Z0-9]", "");

for(int i = 0; i < str.length(); i++) {
    if(Character.isLetterOrDigit((i))){
        if(myString.indexOf(str.charAt(i)) == -1) {
            myString = myString + str.charAt(i);
        }
    }
}
return myString;

}

1 Answers1

-1

Strings are immutable. Just assign the str.replaceAll result to str again, as the method returns a new instance of a String with all your values replaced.

buer
  • 330
  • 4
  • 11