[英]Trimming new line character from a string in java
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? 我错过了什么?
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. 不更改基础值,它返回一个没有前导和结束空白字符的新
String
。 You need to replace your reference 您需要替换您的参考
token = token.trim();
Strings are immutable. 字符串是不可变的。 Change
更改
token.trim();
to 至
token = token.trim();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.