简体   繁体   English

如何从reader.readLine()返回的String中删除unicode空格

[英]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());
    }

would have been faster to just search stackoverflow for this question becoz there are multiple questions on that topic there. 对于这个问题来说,搜索stackoverflow本来会更快,因为那里有关于该主题的多个问题。 well, try this: 好吧,试试这个:

st.replaceAll("\\s","")

check this one here: link 在这里查看这个: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在reader.readLine()期间检测第一行和最后一行? - How to detect first and last line during reader.readLine()? java-服务器未从客户端接收消息(reader.readLine()== null?) - java - server not recieving messages from client (reader.readLine() == null ?) 使用ProcessBuilder执行Linux命令并从reader.readLine()获取NULL,尽管行数为102 - Using ProcessBuilder to execute Linux Command and getting NULL from reader.readLine() although line count is 102 Java 在 reader.readLine() 从服务器读取数据时出现套接字错误 - Java socket error while reading data from server at reader.readLine() 如何在使用reader.readLine()和sc.nextLine()时避免阻塞 - How to avoid blocking while using reader.readLine() and sc.nextLine() Java reader.readLine()不会在文件中返回确切的行 - Java reader.readLine() dosn't return the exact line in the file bufferedReader reader.readline 直接回null - bufferedReader reader.readline directly goes back to null line = reader.readline()结果与文字“F”的行为不同 - line = reader.readline() result does not behave same as a literal “F” 从json响应中删除unicode字符串和空格 - Remove unicode string and spaces from json response 使用“ while(line = reader.readLine()!= null)”时,类型不匹配 - Type mismatch when using “while(line = reader.readLine() != null)”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM