溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java中String字符串刪除空格的方式有哪些

發(fā)布時間:2022-08-13 10:09:33 來源:億速云 閱讀:151 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了java中String字符串刪除空格的方式有哪些的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇java中String字符串刪除空格的方式有哪些文章都會有所收獲,下面我們一起來看看吧。

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

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

  • trim() : 刪除字符串開頭和結(jié)尾的空格。

  • strip() : 刪除字符串開頭和結(jié)尾的空格。

  • stripLeading() : 只刪除字符串開頭的空格

  • stripTrailing() : 只刪除字符串的結(jié)尾的空格

  • replace() : 用新字符替換所有目標(biāo)字符

  • replaceAll() : 將所有匹配的字符替換為新字符。此方法將正則表達式作為輸入,以標(biāo)識需要替換的目標(biāo)子字符串

  • replaceFirst() : 僅將目標(biāo)子字符串的第一次出現(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 '):

java中String字符串刪除空格的方式有哪些

strip()

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

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

而且為了識別這些空格字符,從Java 1.5開始,還在Character類中添加了新的isWhitespace(int)方法。該方法使用unicode來標(biāo)識空格字符。

java中String字符串刪除空格的方式有哪些

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

java中String字符串刪除空格的方式有哪些

java中String字符串刪除空格的方式有哪些

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)來標(biāo)識空白字符。用法也和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中添加的,可以用指定的字符串替換每個目標(biāo)子字符串。

此方法替換所有匹配的目標(biāo)元素

 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()方法,我們可以使用正則表達式來用來識別需要被替換的目標(biāo)字符內(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'

關(guān)于“java中String字符串刪除空格的方式有哪些”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“java中String字符串刪除空格的方式有哪些”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI