Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all non alphabetic characters from a String array in java

Tags:

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 
like image 872
hytriutucx Avatar asked Jun 22 '12 03:06

hytriutucx


People also ask

How can you remove all non-alphanumeric characters from a string?

The approach is to use the String. replaceAll method to replace all the non-alphanumeric characters with an empty string.

How do I remove non characters from a string?

To remove all non-alphanumeric characters from a string, call the replace() method, passing it a regular expression that matches all non-alphanumeric characters as the first parameter and an empty string as the second. The replace method returns a new string with all matches replaced.

How do you remove non-alphanumeric characters?

Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found.

How do I remove all special characters from a string in Java?

You can use a regular expression and replaceAll() method of java. lang. String class to remove all special characters from String.


2 Answers

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(); 
like image 133
n00begon Avatar answered Sep 27 '22 21:09

n00begon


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(); } 
like image 42
paulsm4 Avatar answered Sep 27 '22 19:09

paulsm4