25

I'm trying to write a method that removes all non alphabetic characters from a Java String[] and then convert the String to an lower case string. I've tried using regular expression to replace the occurence of all non alphabetic characters by "" .However, the output that I am getting is not able to do so. Here is the code

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

However if I try to supply an input that has non alphabets (say - or .) the output also consists of them, as they are not removed.

Example Input

A dog is an animal. Animals are not people.

Output that I'm getting

A
dog
is
an
animal.
Animals
are
not
people.

Output that is expected

a
dog
is
an
animal
animals
are
not
people
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
hytriutucx
  • 1,614
  • 6
  • 26
  • 37

9 Answers9

48

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();
Community
  • 1
  • 1
n00begon
  • 3,503
  • 3
  • 29
  • 42
7

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();
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
4

As it already answered , just thought of sharing one more way that was not mentioned here >

 str = str.replaceAll("\\P{Alnum}", "").toLowerCase();
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
saurav
  • 3,424
  • 1
  • 22
  • 33
3

It doesn't work because strings are immutable, you need to set a value e.g.

line[i] = line[i].toLowerCase(); 
Sean F
  • 2,352
  • 3
  • 28
  • 40
3

You must reassign the result of toLowerCase() and replaceAll() back to line[i], since Java String is immutable (its internal value never changes, and the methods in String class will return a new String object instead of modifying the String object).

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
1

A cool (but slightly cumbersome, if you don't like casting) way of doing what you want to do is go through the entire string, index by index, casting each result from String.charAt(index) to (byte), and then checking to see if that byte is either a) in the numeric range of lower-case alphabetic characters (a = 97 to z = 122), in which case cast it back to char and add it to a String, array, or what-have-you, or b) in the numeric range of upper-case alphabetic characters (A = 65 to Z = 90), in which case add 32 (A + 22 = 65 + 32 = 97 = a) and cast that to char and add it in. If it is in neither of those ranges, simply discard it.

algorowara
  • 1,700
  • 1
  • 15
  • 15
1

You can also use Arrays.setAll for this:

Arrays.setAll(array, i -> array[i].replaceAll("[^a-zA-Z]", "").toLowerCase());
Alex - GlassEditor.com
  • 14,957
  • 5
  • 49
  • 49
1

Here is working method

 String name = "Joy.78@,+~'{/>";
 String[] stringArray = name.split("\\W+");
 StringBuilder result = new StringBuilder();
   for (int i = 0; i < stringArray.length; i++) {
    result.append(stringArray[i]);
   }
   String nameNew = result.toString();
    nameNew.toLowerCase();
Pankaj Tungaria
  • 502
  • 3
  • 15
0

public static void solve(String line){

    // trim to remove unwanted spaces
    line= line.trim();

    String[] split = line.split("\\W+");

    // print using for-each
    for (String s : split) {
        System.out.println(s);
    }