欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中String字符串刪除空格的七種方式

 更新時間:2022年08月12日 15:22:11   作者:你鄰座的怪同學(xué)  
在Java中從字符串中刪除空格有很多不同的方法,本文主要介紹了java中String字符串刪除空格的七種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在Java中從字符串中刪除空格有很多不同的方法,如trim,replaceAll等。但是,在JDK 11添加了一些新的功能,如strip、stripLeadingstripTrailing等。

想要從String中移除空格部分,有多少種方法,下面介紹JDK原生自帶的方法,不包含第三方工具類庫中的類似方法

  • trim() : 刪除字符串開頭和結(jié)尾的空格。
  • strip() : 刪除字符串開頭和結(jié)尾的空格。
  • stripLeading() : 只刪除字符串開頭的空格
  • stripTrailing() : 只刪除字符串的結(jié)尾的空格
  • replace() : 用新字符替換所有目標字符
  • replaceAll() : 將所有匹配的字符替換為新字符。此方法將正則表達式作為輸入,以標識需要替換的目標子字符串
  • replaceFirst() : 僅將目標子字符串的第一次出現(xiàn)的字符替換為新的字符串

需要注意的最重要的一點是,在Java中String對象是不可變的,這意味著我們不能修改字符串,因此以上所有的方法我們得到的都是一個新的字符串。

在線運行工具

trim()

trim()是Java開發(fā)人員最常用的刪除字符串開頭和結(jié)尾的空格方法

public class StringTest {
 
    public static void main(String[] args) {
 
        String stringWithSpace = "   Hello word java  ";
 
        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 + "\'");
 
    }
 
}

輸出結(jié)果

Before trim : '   Hello word java  '
After trim : 'Hello word java'

使用trim之后,原字符串中開頭和結(jié)尾部分的空格內(nèi)容都被移除掉了。其實,trim移除的空白字符指的是指ASCII值小于或等于32的任何字符(’ U+0020 '):

strip()

JDK 11的發(fā)行版中,添加了新的strip()方法來刪除字符串中的前導(dǎo)和末尾空格。

trim方法只能針對ASCII值小于等于32的字符進行移除,但是根據(jù)Unicode標準,除了ASCII中的字符以外,還是有很多其他的空白字符的。

而且為了識別這些空格字符,從Java 1.5開始,還在Character類中添加了新的isWhitespace(int)方法。該方法使用unicode來標識空格字符。你可以在http://jkorpela.fi/chars/spaces.html 了解更多關(guān)于unicode空格字符的信息。

而在Java 11中新增的這個strip方法就是使用這個Character.isWhitespace(int)方法來判斷是否為空白字符并刪除它們的:

strip示例

public class StringTest {
 
    public static void main(String args[]) {
 
      String stringWithSpace ='\u2001' + "  Hello word java  " + '\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 + "\'");
 
    }
 
}

結(jié)果

'?' is space : true
Before strip : '?  Hello word java  ?'
After strip : 'Hello word java'

Java 11 中的 strip() 方法要比trim()方法更加強大,他可以移除很多不在ASCII中的空白字符,判斷方式就是通過Character.isWhitespace()方法。

trim() 和 strip() 方法的區(qū)別

trimstrip
Java 1 引入Java 11 引入
使用ASCII使用Unicode值
刪除開頭和結(jié)尾的空白字符刪除開頭和結(jié)尾的空白字符
刪除ASCII值小于或等于’U+0020’或’32’的字符根據(jù)unicode刪除所有空白字符

stripLeading() 和 stripTrailing()

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

public class StringTest {
 
    public static void main(String args[]) {
 
      String stringWithSpace ='\u2001' + "  Hello word java  " + '\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("刪除開頭的空白字符");
 
        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("刪除結(jié)尾的空白字符");
 
        System.out.println("Before stripTrailing : \'" + stringWithSpace + "\'");
 
        String stringAfterTrim = stringWithSpace.stripTrailing();
 
        System.out.println("After stripTrailing : \'" + stringAfterTrim + "\'");
 
    }
 
}

輸出結(jié)果:

'?' is space : true
刪除開頭的空白字符
Before stripLeading : '?  Hello word java  ?'
After stripLeading : 'Hello word java  ?'
刪除結(jié)尾的空白字符
Before stripTrailing : '?  Hello word java  ?'
After stripTrailing : '?  Hello word java'

replace

replace是從java 1.5中添加的,可以用指定的字符串替換每個目標子字符串。

此方法替換所有匹配的目標元素

 public class StringTest {
 
    public static void main(String args[]) {
 
        String stringWithSpace ="  Hello word java  ";
 
        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 + "\'");
 
    }
 
}

結(jié)果:

Before replace : '  Hello word java  '
After replace : 'Hellowordjava'

使用replace方法可以替換掉字符串中的所有空白字符。特別需要注意的是,replace方法和trim方法一樣,只能替換掉ASCII中的空白字符。

replaceAll

replaceAll是Jdk 1.4中添加的最強大的字符串操作方法之一。我們可以將這種方法用于許多目的。
使用replaceAll()方法,我們可以使用正則表達式來用來識別需要被替換的目標字符內(nèi)容。使用正則表達式,就可以實現(xiàn)很多功能,如刪除所有空格,刪除開頭空格,刪除結(jié)尾空格等等。

\s+   所有的空白字符
^\s+      字符串開頭的所有空白字符
\s+$      字符串結(jié)尾的所有空白字符

在java中要添加\我們必須使用轉(zhuǎn)義字符,所以對于\s+ 我們必須使用 \\s+

replaceAll(regex, “”); // 將正則表達式匹配到的內(nèi)容,替換為""

public class StringTest {
 
    public static void main(String args[]) {
 
        String stringWithSpace ="  Hello word java  ";
 
        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 ' ': '  Hello word java  '
After replaceAll with ' ': 'Hellowordjava'
Before replaceAll with '\s+': '  Hello word java  '
After replaceAll with '\s+': 'Hellowordjava'
Before replaceAll with '^\s+': '  Hello word java  '
After replaceAll with '^\s+': 'Hello word java  '
Before replaceAll with '\s+$': '  Hello word java  '
After replaceAll with '\s+$': '  Hello word java'

replaceFirst

replaceFirst方法也是在jdk1.4中添加的,它只將給定正則表達式的第一個匹配項替換為替換字符串。

public class StringTest {
 
    public static void main(String args[]) {
 
        String stringWithSpace ="  Hello word java  ";
 
        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 + "\'");
 
    }
 
}

結(jié)果:

Before replaceFirst with ' ': '  Hello word java  '
After replaceFirst with ' ': ' Hello word java  '
Before replaceFirst with '\s+': '  Hello word java  '
After replaceFirst with '\s+': 'Hello word java  '
Before replaceFirst with '^\s+': '  Hello word java  '
After replaceFirst with '^\s+': 'Hello word java  '
Before replaceFirst with '\s+$': '  Hello word java  '
After replaceFirst with '\s+$': '  Hello word java'

總結(jié)

想要直接移除掉字符串開頭的空白字符,可以使用stripLeading、replaceAllreplaceFirst

想要直接移除掉字符串末尾的空白字符,可以使用stripTrailingreplaceAllreplaceFirst

想要同時移除掉字符串開頭和結(jié)尾的空白字符,可以使用striptrim

想要移除掉字符串中的所有空白字符,可以使用replacereplaceAll

而Java 11種新增的strip、stripTrailing以及stripLeading方法,可以移除的字符要比其他方法多,他可以移除的空白字符不僅僅局限于ASCII中的字符,而是Unicode中的所有空白字符,具體判斷方式可以使用Character.isWhitespace進行判斷。

到此這篇關(guān)于java中String字符串刪除空格的七種方式的文章就介紹到這了,更多相關(guān)java String字符串刪除空格 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MapReduce核心思想圖文詳解

    MapReduce核心思想圖文詳解

    今天小編就為大家分享一篇關(guān)于MapReduce核心思想圖文詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 簡單了解java類的初始化以及類的實例化

    簡單了解java類的初始化以及類的實例化

    這篇文章主要介紹了簡單了解java類的初始化以及類的實例化,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • MyBatis實現(xiàn)配置加載的步驟

    MyBatis實現(xiàn)配置加載的步驟

    本文主要介紹了MyBatis實現(xiàn)配置加載的步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java多線程中的wait/notify通信模式實例詳解

    Java多線程中的wait/notify通信模式實例詳解

    這篇文章主要給大家介紹了關(guān)于Java多線程中wait/notify通信模式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • spring redis 如何實現(xiàn)模糊查找key

    spring redis 如何實現(xiàn)模糊查找key

    這篇文章主要介紹了spring redis 如何實現(xiàn)模糊查找key的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring MVC參數(shù)傳遞中文亂碼解決方法分享

    Spring MVC參數(shù)傳遞中文亂碼解決方法分享

    這篇文章主要介紹了Spring MVC參數(shù)傳遞中文亂碼解決方法分享,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Java整合RabbitMQ實現(xiàn)五種常見消費模型

    Java整合RabbitMQ實現(xiàn)五種常見消費模型

    本文將深入介紹RabbitMQ的五種常見消費模型,包括簡單隊列模型、工作隊列模型、發(fā)布/訂閱模型、路由模型和主題模型,刪除線格式并探討它們各自的優(yōu)缺點和適用場景,感興趣的可以了解一下
    2023-11-11
  • Java Swing JPasswordField密碼框的實現(xiàn)示例

    Java Swing JPasswordField密碼框的實現(xiàn)示例

    這篇文章主要介紹了Java Swing JPasswordField密碼框的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Spring Boot 直接用jar運行項目的方法

    Spring Boot 直接用jar運行項目的方法

    這篇文章主要介紹了Spring Boot 直接用jar運行項目的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2018-02-02
  • 在Android的應(yīng)用中實現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法

    在Android的應(yīng)用中實現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法

    這篇文章主要介紹了在Android的應(yīng)用中實現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法,一定程度上有助于提高安卓程序的使用體驗,需要的朋友可以參考下
    2015-07-07

最新評論