從String中移除空白字符的多種方式!?差別竟然這麼大!

2021-02-19 imooc編程手記

字符串,是Java中最常用的一個數據類型了。我們在日常開發時候會經常使用字符串做很多的操作。比如字符串的拼接、截斷、替換等。
這一篇文章,我們介紹一個比較常見又容易被忽略的一個操作,那就是移除字符串中的空格。其實,在Java中從字符串中刪除空格有很多不同的方法,如trim,replaceAll等。但是,在Java 11添加了一些新的功能,如strip、stripLeading、stripTrailing等。大多數時候,我們只是使用trim方法來刪除多餘的空格。但是好像很多人並沒有去思考過,是否有更好的方式呢?當然,trim()在大多數情況下都工作得很好,但是Java中有許多不同的方法。每一種都有自己的優點和缺點。我們如何決定哪種方法最適合我們呢?接下來我們將介紹幾種方法,並對比下他們的區別和優缺點等。首先,我們來看一下,想要從String中移除空格部分,有多少種方法,作者根據經驗,總結了以下7種(JDK原生自帶的方法,不包含第三方工具類庫中的類似方法):stripLeading() : 只刪除字符串開頭的空格stripTrailing() : 只刪除字符串的結尾的空格replaceAll() : 將所有匹配的字符替換為新字符。此方法將正則表達式作為輸入,以標識需要替換的目標子字符串replaceFirst() : 僅將目標子字符串的第一次出現的字符替換為新的字符串需要注意的最重要的一點是,在Java中String對象是不可變的,這意味著我們不能修改字符串,因此以上所有的方法我們得到的都是一個新的字符串。接下啦,我們分別針對以上這幾個方法學習下用法,了解下其特性。PS:本文代碼都是使用在線運行工具(https://www.jdoodle.com/online-java-compiler/ )執行的,因為我的測試機並未安裝Java 11,並且Unicode字符也不完整。如果大家也想實驗,建議使用在線工具,選擇對應的JDK即可。trim()是Java開發人員最常用的刪除字符串開頭和結尾的空格方法。其用法也比較簡單:

public class StringTest {

    public static void main(String[] args) {

        String stringWithSpace = "   Hollis   Is   A   Java   Coder   ";

        StringTest.trimTest(stringWithSpace);

    }

    private static void trimTest(String stringWithSpace){

        System.out.println("Before trim : \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.trim();

        System.out.println("After trim : \'" + stringAfterTrim + "\'");

    }

}

Before trim : '   Hollis   Is   A   Java   Coder   '

After trim : 'Hollis   Is   A   Java   Coder'

如上,使用trim之後,原字符串中開頭和結尾部分的空格內容都被移除掉了。但是不知道大家有沒有思考過,trim方法移除的空白內容都包含哪些東西?除了空格以外,還有其他的字符嗎?其實,trim移除的空白字符指的是指ASCII值小於或等於32的任何字符(' U+0020 '):不知道大家有沒有注意到,在Java 11的發行版中,添加了新的strip()方法來刪除字符串中的前導和末尾空格。已經有了一個trim方法,為什麼還要新增一個strip呢?這其實是是因為trim方法只能針對ASCII值小於等於32的字符進行移除,但是根據Unicode標準,除了ASCII中的字符以外,還是有很多其他的空白字符的。而且為了識別這些空格字符,從Java 1.5開始,還在Character類中添加了新的isWhitespace(int)方法。該方法使用unicode來標識空格字符。你可以在http://jkorpela.fi/chars/spaces.html 了解更多關於unicode空格字符的信息。而在Java 11中新增的這個strip方法就是使用這個Character.isWhitespace(int)方法來判斷是否為空白字符並刪除它們的:

public class StringTest {

    public static void main(String args[]) {

      String stringWithSpace ='\u2001' + "  Hollis   Is   A   Java   Coder  " + '\u2001';

        System.out.println("'" + '\u2001' + "' is space : " +  Character.isWhitespace('\u2001'));

        StringTest.stripTest(stringWithSpace);

    }

    private static void stripTest(String stringWithSpace){

        System.out.println("Before strip : \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.strip();

        System.out.println("After strip : \'" + stringAfterTrim + "\'");

    }

}

我們在字符串前後都增加了一個特殊的字符\u2001,這個字符是不在ASCII中的,經過Character.isWhitespace判斷他是一個空白字符。然後使用strip進行處理,輸出結果如下:

' ' is space : true

Before strip : '   Hollis   Is   A   Java   Coder   '

After strip : 'Hollis   Is   A   Java   Coder'

所以,Java 11 中的 strip 方法要比trim方法更加強大,他可以移除很多不在ASCII中的空白字符,判斷方式就是通過Character.isWhitespace方法。上面我們介紹了兩個都可以移除字符串開頭和結尾的方法,分別是trim 和 strip,再來對比下他們的區別:


stripLeading() 和 stripTrailing()stripLeading()和stripTrailing()方法也都是在Java 11中添加的。作用分別是刪除字符串的開頭的空格以及刪除字符串的末尾的空格。與strip方法類似,stripLeading、stripTrailing也使用Character.isWhitespace(int)來標識空白字符。用法也和strip類似:

public class StringTest {

    public static void main(String args[]) {

      String stringWithSpace ='\u2001' + "  Hollis   Is   A   Java   Coder  " + '\u2001';

        System.out.println("'" + '\u2001' + "' is space : " +  Character.isWhitespace('\u2001'));

        StringTest.stripLeadingTest(stringWithSpace);

        StringTest.stripTrailingTest(stringWithSpace);

    }


    private static void stripLeadingTest(String stringWithSpace){

        System.out.println("Before stripLeading : \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.stripLeading();

        System.out.println("After stripLeading : \'" + stringAfterTrim + "\'");

    }


     private static void stripTrailingTest(String stringWithSpace){

        System.out.println("Before stripTrailing : \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.stripTrailing();

        System.out.println("After stripTrailing : \'" + stringAfterTrim + "\'");

    }

}

' ' is space : true

Before stripLeading : '   Hollis   Is   A   Java   Coder   '

After stripLeading : 'Hollis   Is   A   Java   Coder   '

Before stripTrailing : '   Hollis   Is   A   Java   Coder   '

After stripTrailing : '   Hollis   Is   A   Java   Coder'

移除字符串中的空白字符,除了使用trim、strip以外,還有一個辦法,那就是使用replace方法把其中的空白字符替換掉。replace是從java 1.5中添加的,可以用指定的字符串替換每個目標子字符串。

 public class StringTest {

    public static void main(String args[]) {

        String stringWithSpace ="  Hollis   Is   A   Java   Coder  ";

        StringTest.replaceTest(stringWithSpace);

    }



    private static void replaceTest(String stringWithSpace){

        System.out.println("Before replace : \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.replace(" ", "");

        System.out.println("After replace : \'" + stringAfterTrim + "\'");

    }

}

Before replace : '  Hollis   Is   A   Java   Coder  '

After replace : 'HollisIsAJavaCoder'

可見,以上使用replace方法可以替換掉字符串中的所有空白字符。特別需要注意的是,replace方法和trim方法一樣,只能替換掉ASCII中的空白字符。replaceAll是Java 1.4中添加的最強大的字符串操作方法之一。我們可以將這種方法用於許多目的。使用replaceAll()方法,我們可以使用正則表達式來用來識別需要被替換的目標字符內容。使用正則表達式,就可以實現很多功能,如刪除所有空格,刪除開頭空格,刪除結尾空格等等。我們只需要用正確的替換參數創建正確的正則表達式。一些正則表達式的例子如下:

\s+   所有的空白字符

^\s+      字符串開頭的所有空白字符

\s+$      字符串結尾的所有空白字符

注意,在java中要添加/我們必須使用轉義字符,所以對於\s+ 我們必須使用 \\s+

public class StringTest {

    public static void main(String args[]) {

        String stringWithSpace ="  Hollis   Is   A   Java   Coder  ";

        StringTest.replaceAllTest(stringWithSpace," ");

        StringTest.replaceAllTest(stringWithSpace,"\\s+");

        StringTest.replaceAllTest(stringWithSpace,"^\\s+");

        StringTest.replaceAllTest(stringWithSpace,"\\s+$");

    }


    private static void replaceAllTest(String stringWithSpace,String regex){

        System.out.println("Before replaceAll with '"+ regex +"': \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.replaceAll(regex, "");

        System.out.println("After replaceAll with '"+ regex +"': \'" + stringAfterTrim + "\'");

    }

}

Before replaceAll with ' ': '  Hollis   Is   A   Java   Coder  '

After replaceAll with ' ': 'HollisIsAJavaCoder'

Before replaceAll with '\s+': '  Hollis   Is   A   Java   Coder  '

After replaceAll with '\s+': 'HollisIsAJavaCoder'

Before replaceAll with '^\s+': '  Hollis   Is   A   Java   Coder  '

After replaceAll with '^\s+': 'Hollis   Is   A   Java   Coder  '

Before replaceAll with '\s+$': '  Hollis   Is   A   Java   Coder  '

After replaceAll with '\s+$': '  Hollis   Is   A   Java   Coder'

正如我們所看到的,如果將replaceAll()與適當的正則表達式一起使用,它將是非常強大的方法。replaceFirst方法也是在java 1.4中添加的,它只將給定正則表達式的第一個匹配項替換為替換字符串。如果您只需要替換第一次出現的情況,那麼這個方法非常有用。例如,如果我們只需要刪除前導空格,我們可以使用\\s+或^\\s+。我們還可以通過使用\\s+$正則表達式使用此方法來刪除末尾空格。因為這個表達式將只匹配行的最後一個空格。因此最後的空格被認為是這個方法的第一個匹配。

public class StringTest {

    public static void main(String args[]) {

        String stringWithSpace ="  Hollis   Is   A   Java   Coder  ";

        StringTest.replaceFirstTest(stringWithSpace," ");

        StringTest.replaceFirstTest(stringWithSpace,"\\s+");

        StringTest.replaceFirstTest(stringWithSpace,"^\\s+");

        StringTest.replaceFirstTest(stringWithSpace,"\\s+$");

    }


    private static void replaceFirstTest(String stringWithSpace,String regex){

        System.out.println("Before replaceFirst with '"+ regex +"': \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.replaceFirst(regex, "");

        System.out.println("After replaceFirst with '"+ regex +"': \'" + stringAfterTrim + "\'");

    }

}

Before replaceFirst with ' ': '  Hollis   Is   A   Java   Coder  '

After replaceFirst with ' ': ' Hollis   Is   A   Java   Coder  '

Before replaceFirst with '\s+': '  Hollis   Is   A   Java   Coder  '

After replaceFirst with '\s+': 'Hollis   Is   A   Java   Coder  '

Before replaceFirst with '^\s+': '  Hollis   Is   A   Java   Coder  '

After replaceFirst with '^\s+': 'Hollis   Is   A   Java   Coder  '

Before replaceFirst with '\s+$': '  Hollis   Is   A   Java   Coder  '

After replaceFirst with '\s+$': '  Hollis   Is   A   Java   Coder'

想要直接移除掉字符串開頭的空白字符,可以使用stripLeading、replaceAll和replaceFirst想要直接移除掉字符串末尾的空白字符,可以使用stripTrailing、replaceAll和replaceFirst想要同時移除掉字符串開頭和結尾的空白字符,可以使用strip、trim想要移除掉字符串中的所有空白字符,可以使用replace和replaceAll而Java 11種新增的strip、stripTrailing以及stripLeading方法,可以移除的字符要比其他方法多,他可以移除的空白字符不僅僅局限於ASCII中的字符,而是Unicode中的所有空白字符,具體判斷方式可以使用Character.isWhitespace進行判斷。

相關焦點

  • PHP處理字符中的emoji表情【判斷/移除/存儲】
    utf-8 編碼的常用中文字符佔用 3 個字節。一、判斷字符串中是否含有 emoji 表情三個 PHP 內置函數:mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )string mb_substr
  • R 字符串之 stringr
    R 字符串之 stringr前言昨天我們介紹 R 數據處理的時候,對字符串的操作都是用自帶的函數。雖然 R 的字符串並不是它的強項,看起來也不是那麼的優雅,但是字符串在數據處理和清洗過程中還是扮演者較為重要的角色。
  • C/C++中字符串string類型
    兩種風格1.C風格字符串: char 變量名[] = "字符串值"示例:int main() {char str1[] = ": string 變量名 = "字符串值"int main() { string str = "hello world"; cout << str << endl; system("pause");
  • 深入剖析go中字符串的編碼問題——特殊字符的string怎麼轉byte?
    ,結果發現太複雜就跑去逛知乎了,然後就發現了一個非常有意思的提問「golang 特殊字符的string怎麼轉成[]byte?」。直到實際運行的時候才發現上圖的特殊字符是『』(如果無法展示,記住該特殊字符的unicode是\u0081),並不是英文中的句號。
  • 學習一下Java 11新增的String實用方法
    0的字符串做操作,會返回空字符串。,但不能處理尾部空白。因為trim()方法不能識別Unicode的空白字符,不認為'\u2005'是一個空白字符。 ## 4 isBlank() 如果字符串為空或只包含空格,則返回true。否則返回false。
  • 字符串輸出的4種方式:Python String Formatting Best Practices
    Python 字符串格式化技巧和最佳實踐。還記得Python的禪宗,以及 "在Python中應該有一種明顯的方法來做某事 "嗎?當你發現在Python中進行字符串格式化有四種主要方法時,你可能會撓頭。在本教程中,您將學習Python中字符串格式化的四種主要方法,以及它們的優缺點。您還會得到一個簡單的經驗法則,即如何在自己的程序中選擇最佳的通用字符串格式化方法。
  • String字符串常用方法
    1、IndexOf方法:確定指定字符串在字符串中的索引,如果在字符串中找到指定字符,則返回其索引,否則返回-1。
  • C++之旅-string
    前言標準庫類型string表示可變長字符序列,使用之前需要包含string頭文件,它定義在命名空間std中。
  • [編程基礎] Python格式化字符串常量f-string總結
    Python格式化字符串常量f-string總結本文主要總結在Python中如何使用格式化字符串常量f-string(Formatted string
  • python中字符串的基本操作匯總
    字符串是文本操作的核心,在python中字符串是string類的實例,在string模塊中,定義了很多的常量>>> import string>>> string.ascii_letters'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  • Ruby 字符串(String)
    15str.chop移除 str 中的最後一個字符。16str.chop!與 chop 相同,但是 str 會發生變化並返回。17str.concat(other_str)連接 other_str 到 str。18str.count(str, ...)
  • string字符串的比較
    string字符串的比較運算符有如下幾種:>大於;>=大於等於;<小於小於等於;==等於;如下所示是等於運算符的一個實例:#include<iostream>#include<windows.h>#include<string
  • L2-數據結構-第05課 字符串string
    如果 s 是一個 string 對象且 s 不空,則 s[0] 就是字符串的第一個字符, s[1] 就表示第二個字符,而 s[s.size() - 1] 則表示 s 的最後一個字符。 字符串 "ABC"str.insert(2, "ABC", 1)——在str下標為2的位置添加 字符串 "ABC" 中 1個 字符str.insert(2, "ABC", 1, 1)——在str下標為2的位置添加 字符串 "ABC" 中從位置 1 開始的 1 個字符erase(下標), clear() 和 empty()
  • foreach中遍歷list中string.contains的使用
    現在講有三個string類型list,列表中數據如下;三者的關係如圖所示,這裡需要得到圖中紅色部分;主要B、C中包含A的的字符串即可,不需要完全相同;這裡如果直接按照列表中進行遍歷可以直接通過foreach進行遍歷;代碼1:using System.Collections
  • Tcl學習:string compare命令對字符串的比較
    打開APP Tcl學習:string compare命令對字符串的比較 工程師李察 發表於 2018-09-23 10:10:00
  • LeetCode-87.擾亂字符串(Scramble String)
    英文版新版只給了兩個轉換規則We can scramble a string s to get a string t using the following algorithm:If the length of the string is 1, stop.If the length of the string is > 1, do the following
  • 為什麼 Python 的 f-string 可以連接字符串與數字?
    由此,我們要引出一個問題:如何在不作顯式類型轉化的情況下,進行字符串與數字類型的拼接呢?在《詳解Python拼接字符串的七種方式》這篇文章中,它梳理了七種拼接字符串的寫法,我們可以逐個來試驗一下。幾種字符串拼接方式:1、格式化類:%、format()、template2、拼接類:+、()、join()3、插值類:f-string為了節省篇幅,此處直接把可以順利拼接的 4 種寫法羅列如下:>>> "%s
  • python 通關字符串操作方法詳解-大量案例
    string.lstrip()截掉 string 左邊的空格string.maketrans(intab, outtab])maketrans() 方法用於創建字符映射的轉換表,對於接受兩個參數的最簡單的調用方式
  • C++學習,關於字符串 remove_ctrl() 函數,你知道嗎?
    函數的功能是從一個由 ASCII 字符組成的字符串中移除控制字符。看起來它似乎很無辜,但是出於多種原因,這種寫法的函數確實性能非常糟糕。而代碼清單的 remove_ctrl() 函數的執行時間在程序整體執行時間中所佔的比例非常大。
  • C++ string 類詳解
    學習了C++string類以後,發現原來字符串處理可以這麼簡單。那麼本文會帶領大家回顧一下string類的相關操作,查缺補漏。—————【以下是正文】—————字符串是存儲在內存的連續字節中的一系列字符。C++ 處理字符串的方式有兩種,一種來自 C 語言,常被稱為 C-風格字符串,另一種是基於 string 類庫的字符串處理方式。