[英]how to remove unicode spaces from String returned by reader.readLine()
I when i use reader.readLine(), the string length is always 80 chars and after the main string unicode spaces are padded up. 我使用reader.readLine()时,字符串长度始终为80个字符,并在主字符串unicode空格填充之后。 Is there a way to remove those unwanted characters.
有没有办法删除那些不需要的字符。 (java.io.RandomAccessFile reader) String.trim is not working on this
(java.io.RandomAccessFile reader)String.trim没有在这方面工作
You can use StringUtils.strip from Commons Lang. 您可以使用Commons Lang的StringUtils.strip 。 It is Unicode-aware.
它具有Unicode感知能力。
You can write a custom method in Java to remove the Unicode space characters , using Character.isWhitespace(char) and Character.isSpaceChar(char) methods, for your specific purpose. 您可以使用Character.isWhitespace(char)和Character.isSpaceChar(char)方法在Java中编写自定义方法以删除Unicode空格字符,以用于特定目的。
The Spring framework has a StringUtils class with a trimWhitespace(String) method which appears to be based on Character.isWhitespace(char)
from the source code here . Spring框架有一个StringUtils类,其中包含一个trimWhitespace(String)方法,该方法似乎基于来自此处源代码的
Character.isWhitespace(char)
。
use Google Guava 使用Google Guava
CharMatcher.WHITESPACE.trimFrom(source);
or try this https://gist.github.com/michalbcz/4861a2b8ed73bb73764e909b87664cb2 或试试这个https://gist.github.com/michalbcz/4861a2b8ed73bb73764e909b87664cb2
If you do not want a big libs. 如果你不想要一个大的libs。 Just use:
只需使用:
str.replaceAll("^[\\\\s\\\\p{Z}]+|[\\\\s\\\\p{Z}]+$", "");
Testing 测试
public static String trim(String str) {
return str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");
}
public static void main(String[] args) {
System.out.println(trim("\t tes ting \u00a0").length());
System.out.println(trim("\t testing \u00a0").length());
System.out.println(trim("tes ting \u00a0").length());
System.out.println(trim("\t tes ting").length());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.