26

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
1
  • 1
    Here is a sample run to check the method ideone.com/XOugF Commented Jun 22, 2012 at 3:24

9 Answers 9

50

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();
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

4

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

 str = str.replaceAll("\\P{Alnum}", "").toLowerCase();

1 Comment

Though this is wider than just the latin letters matched by the OPs code; that may or may not be what is needed.
3

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

line[i] = line[i].toLowerCase(); 

2 Comments

Sean, you missed the replaceAll call there.
i was going to edit it in but once i submitted and 3 other responses existed saying the same thing i didnt see the point.
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).

Comments

2

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

Comments

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.

Comments

1

You can also use Arrays.setAll for this:

Arrays.setAll(array, i -> array[i].replaceAll("[^a-zA-Z]", "").toLowerCase());

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.