簡體   English   中英

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

[英]how to remove unicode spaces from String returned by reader.readLine()

我使用reader.readLine()時,字符串長度始終為80個字符,並在主字符串unicode空格填充之后。 有沒有辦法刪除那些不需要的字符。 (java.io.RandomAccessFile reader)String.trim沒有在這方面工作

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

您可以使用Character.isWhitespace(char)Character.isSpaceChar(char)方法在Java中編寫自定義方法以刪除Unicode空格字符,以用於特定目的。

Spring框架有一個StringUtils類,其中包含一個trimWhitespace(String)方法,該方法似乎基於來自此處源代碼的 Character.isWhitespace(char)

使用Google Guava

CharMatcher.WHITESPACE.trimFrom(source);

或試試這個https://gist.github.com/michalbcz/4861a2b8ed73bb73764e909b87664cb2

如果你不想要一個大的libs。 只需使用:

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

對於這個問題來說,搜索stackoverflow本來會更快,因為那里有關於該主題的多個問題。 好吧,試試這個:

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

在這里查看這個: 鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM