[英]Why can't I Remove WhiteSpaces in Java?
I'm desperately trying to remove white spaces from a String (that late i want to be able to convert into an int) but i can't seem to get it all right. 我拼命地试图从字符串中删除空格(后来我希望能够将其转换为int),但是我似乎无法正常运行。
String input;
if(GamePlay.GameStart == true){
input = Terminal.askString("Welcome to MASTERMIND! \n Please Give in Your Game Attributes \n");
input.replaceAll("\\s","");
}else
input = Terminal.askString("");
if (input.equals("Quit") || input.equals("quit")){
Quit();
}
else if(GamePlay.GameStart == true){
System.out.println(input); .......(code follows)
Can you please tell me,what it is that i'm doing wrong? 你能告诉我,我做错了什么吗? PS:I've tried \\W" and \\S" too
PS:我也尝试过\\ W“和\\ S”
replace 更换
input.replaceAll("\\s","");
with 与
input = input.replaceAll("\\s","");
It will work, because strings are immutable, so replaceAll()
won't change your string object, it will return a new one. 它将起作用,因为字符串是不可变的,所以
replaceAll()
不会更改您的字符串对象,它将返回一个新对象。 So you assign your variable input
to the string returned by input.replaceAll("\\\\s","");
因此,您将变量
input
分配给input.replaceAll("\\\\s","");
返回的字符串input.replaceAll("\\\\s","");
Also, you should try to follow the Java naming conventions, and have your fields and variables start with a lowercase letters. 另外,您应尝试遵循Java命名约定,并使字段和变量以小写字母开头。
And, you can also replace 而且,您也可以更换
if(GamePlay.GameStart == true) {
with 与
if(GamePlay.GameStart) {
because in your version you compare the value of GamePlay.GameStart
with true
, and only execute the if
block if that evaluation is true
, whereas in my version, the ìf
block is executed if GamePlay.GameStart
is true
(although the compiler probably optimizes it away anyway). 因为在您的版本中,您将
GamePlay.GameStart
的值与true
进行比较,并且仅在评估为true
时才执行if
块,而在我的版本中,如果GamePlay.GameStart
为true
则执行ìf
块(尽管编译器可能对其进行了优化)无论如何)。
On another note, you can also replace 另一方面,您也可以替换
if (input.equals("Quit") || input.equals("quit")){
with 与
if (input.equalsIgnoreCase("Quit")) {
because, well I think it's obvious. 因为,我认为这很明显。
Strings are immutable. 字符串是不可变的。 You need to assign your result of the newly created
String
returned by replaceAll
: 您需要分配由
replaceAll
返回的新创建的String
结果:
input = input.replaceAll("\\s","");
input.replaceAll("\\\\s","");
returns a string with the spaces removed. 返回删除了空格的字符串。 But if you don't keep a reference to it you won't be able to see the result.
但是,如果不保留对它的引用,您将看不到结果。 You probably meant:
您可能的意思是:
input = input.replaceAll("\\s","");
This is a very common mistake for beginners. 对于初学者来说,这是一个非常常见的错误。
As others have mentioned, you need to know that replaceAll
returns a new string and doesn't change the current one, since strings are immutable in Java. 正如其他人提到的那样,您需要知道
replaceAll
返回一个新字符串并且不更改当前字符串,因为字符串在Java中是不可变的。
So use: 因此使用:
input=input.replaceAll("\\s","");
If you wish, you can have assistant plugins to help you with such bugs, such as findBugs and codePro . 如果愿意,可以使用助手插件来帮助您解决此类错误,例如findBugs和codePro 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.