从java中删除字符串数组中的所有非字母字符。

时间:2022-12-28 20:22:15

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

我尝试编写一个方法,从Java字符串中删除所有非字母字符[],然后将字符串转换为小写字符串。我尝试使用正则表达式将所有非字母字符的出现替换为“”,但是,我得到的输出不能这样做。这是代码

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.

但是,如果我尝试提供一个具有非字母表(例如- or .)的输入,输出也包含这些输入,因为它们没有被删除。

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

6 个解决方案

#1


32  

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

#2


5  

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

您需要将regex的结果分配回行[i]。

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

#3


2  

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

因为字符串是不可变的,你需要设置一个值。

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

#4


2  

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).

您必须重新将toLowerCase()和replaceAll()的结果重新分配到第[i]行,因为Java字符串是不可变的(它的内部值永不改变,String类中的方法将返回一个新的字符串对象,而不是修改字符串对象)。

#5


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.

很酷(但有点麻烦,如果你不喜欢铸造)的方式做你要做的是通过整个字符串,指数指数,铸造每个结果从String.charAt(指数)(字节),然后检查数字范围的字节是)小写字母字符(= 97 z = 122),在这种情况下丢回字符并将它添加到一个字符串,数组,或其它,或b)大写字母字符的数字范围(= 65 z = 90),在这种情况下,添加32 (A + 22 = 65 + 32 = 97 = A)并将其转换为char并添加进去。如果它不在这两个范围内,就丢弃它。

#6


1  

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

就像它已经回答的那样,只是想再分享一个在这里没有提到的>。

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

#1


32  

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

#2


5  

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

您需要将regex的结果分配回行[i]。

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

#3


2  

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

因为字符串是不可变的,你需要设置一个值。

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

#4


2  

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).

您必须重新将toLowerCase()和replaceAll()的结果重新分配到第[i]行,因为Java字符串是不可变的(它的内部值永不改变,String类中的方法将返回一个新的字符串对象,而不是修改字符串对象)。

#5


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.

很酷(但有点麻烦,如果你不喜欢铸造)的方式做你要做的是通过整个字符串,指数指数,铸造每个结果从String.charAt(指数)(字节),然后检查数字范围的字节是)小写字母字符(= 97 z = 122),在这种情况下丢回字符并将它添加到一个字符串,数组,或其它,或b)大写字母字符的数字范围(= 65 z = 90),在这种情况下,添加32 (A + 22 = 65 + 32 = 97 = A)并将其转换为char并添加进去。如果它不在这两个范围内,就丢弃它。

#6


1  

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

就像它已经回答的那样,只是想再分享一个在这里没有提到的>。

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