[英]Digital type the string " trim " and " replaceAll " can't remove the last spaces
I want to remove all spaces in a string of numeric types, I used the function such as trim()
, replaceall(" ", "")
, but the last space always goes to off.我想删除数字类型字符串中的所有空格,我使用了诸如
trim()
、 replaceall(" ", "")
类的函数,但最后一个空格总是关闭。
code:代码:
String str = " 13554912187 ";
//String str2 = str.replaceAll(" ", "");
String str2 = str.trim();
System.out.println(str);
System.out.println(str.length());
System.out.println(str2);
System.out.println(str2.length());
I want to know why.我想知道为什么。
Make sure the trailing space is actually considered whitespace by String.trim()
- only characters with code less than or equal to \
are considered whitespace - see String.trim()
documentation .确保尾随空格实际上被
String.trim()
视为空格 - 只有代码小于或等于\
的字符才被视为空格 - 请参阅String.trim()
文档。
Some possible ways to check this:一些可能的方法来检查这个:
String
instance for that last spaceString
实例中存储了什么值String str2 = str.trim(); String str2 = str.trim();
trim() method remove space before and after the word. trim() 方法删除单词前后的空格。
like喜欢
String s=" 45654 65 ";
System.out.println(s.startsWith(" "));
System.out.println(s.endsWith(" "));
s=s.trim();
System.out.println(s.startsWith(" "));
System.out.println(s.endsWith(" "));
output:输出:
true
true
false
false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.