Remove all non alphabetic characters from a String array in java

You need to assign the result of your regex back to lines[i].

for ( int i = 0; i < line.length; i++) {
  line[i] = line[i].replaceAll("[^a-zA-Z]", "").toLowerCase();
}

The problem is your changes are not being stored because Strings are immutable. Each of the method calls is returning a new String representing the change, with the current String staying the same. You just need to store the returned String back into the array.

line[i] = line[i].replaceAll("[^a-zA-Z]", "");
line[i] = line[i].toLowerCase();

Because the each method is returning a String you can chain your method calls together. This will perform the second method call on the result of the first, allowing you to do both actions in one line.

line[i] = line[i].replaceAll("[^a-zA-Z]", "").toLowerCase();

Tags:

Java

String

Regex