Can';t替换Java字符串中的标点符号
这是我的密码:Can';t替换Java字符串中的标点符号,java,android,string,replace,Java,Android,String,Replace,这是我的密码: public void storeWords(String sentence) { String[] wordlist = sentence.split("\\s+"); int stringLength = wordlist.length; for(int j = 0; j < stringLength; j++) { wordlist[j].replaceAll("[^a-zA-Z ]", ""); Sy
public void storeWords(String sentence)
{
String[] wordlist = sentence.split("\\s+");
int stringLength = wordlist.length;
for(int j = 0; j < stringLength; j++)
{
wordlist[j].replaceAll("[^a-zA-Z ]", "");
System.out.println(wordlist[j]);
}
}
当我将其更改为此时,您没有指定的结果-
wordlist[j] = wordlist[j].replaceAll("[^a-zA-Z ]", "");
我得到输出(与您的输入)
注意,Javadoc返回的是“结果字符串”
字符串是不可变的,必须执行以下操作:
wordlist[j] = wordlist[j].replaceAll("[^a-zA-Z ]", "");
(不可变意味着一旦赋值,它们就无法更改,因此您需要进行新赋值才能更改字符串的值)replaceAll到底做什么?天哪,我怎么总是忘记这一点。有时,我希望我是用C或C++。谢谢当我的计时器关闭时,我会接受它。
If
somebody
falls
and
has
no
protection
do
they
get
hurt
wordlist[j] = wordlist[j].replaceAll("[^a-zA-Z ]", "");