The output of below program:
以下程序的输出:
public class TestClass {
public static void main(final String[] args){
String token = "null\n";
token.trim();
System.out.println("*");
System.out.println(token);
System.out.println("*");
}
}
is:
是:
*
null
*
However
然而
How to remove newlines from beginning and end of a string (Java)?
如何从字符串的开头和结尾删除换行符(Java)?
says otherwise.
否则说。
What am I missing?
我错过了什么?
2 个解决方案
#1
21
Since String
is immutable
因为String是不可变的
token.trim();
doesn't change the underlying value, it returns a new String
without the leading and ending whitespace characters. You need to replace your reference
不更改基础值,它返回一个没有前导和结束空白字符的新String。您需要替换您的参考
token = token.trim();
#2
13
Strings are immutable. Change
字符串是不可变的。更改
token.trim();
to
至
token = token.trim();
#1
21
Since String
is immutable
因为String是不可变的
token.trim();
doesn't change the underlying value, it returns a new String
without the leading and ending whitespace characters. You need to replace your reference
不更改基础值,它返回一个没有前导和结束空白字符的新String。您需要替换您的参考
token = token.trim();
#2
13
Strings are immutable. Change
字符串是不可变的。更改
token.trim();
to
至
token = token.trim();