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

时间:2013-07-15 11:05:18

标签: java android

当我使用reader.readLine()时,字符串长度始终为80个字符,并在主字符串unicode空格填充之后。 有没有办法删除那些不需要的字符。 (java.io.RandomAccessFile阅读器) String.trim不适用于此

5 个答案:

答案 0 :(得分:7)

您可以使用Commons Lang的StringUtils.strip。它具有Unicode感知能力。

答案 1 :(得分:3)

您可以使用Character.isWhitespace(char)Character.isSpaceChar(char)方法在Java中编写自定义方法来删除Unicode空格字符,以用于特定目的。

Spring框架有一个StringUtils类,其trimWhitespace(String)方法似乎基于source code here中的Character.isWhitespace(char)

答案 2 :(得分:0)

使用Google Guava

[x.strip() for x in testString[7:].split(',')]

或尝试此https://gist.github.com/michalbcz/4861a2b8ed73bb73764e909b87664cb2

答案 3 :(得分:0)

如果您不想要大图书馆。只需使用:

str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");

测试

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

答案 4 :(得分:-2)

在这个问题上搜索stackoverflow会更快,因为那里有关于该主题的多个问题。好吧,试试这个:

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

在此处查看此内容:link