国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Java 字符串中刪除子字符串的9種方法詳細內(nèi)容(remove substring from String)

這篇具有很好參考價值的文章主要介紹了Java 字符串中刪除子字符串的9種方法詳細內(nèi)容(remove substring from String)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

總結(jié):

Java 中的字符串中刪除子字符串有以下方法:
1.Using replace method使用替換方法
2.Using Charsequence使用字符序列
3.Replace the Substring with an empty string用空字符串替換子字符串
4.Using String’s replaceFirst method使用 String 的 replaceFirst 方法
5.replaceFirst() method
6.Using replaceAll method 使用 replaceAll 方法
7.replaceAll() method
8.Using String Builder’s delete() method使用 String Builder 的 delete() 方法
9.Using StringBuilder replace() Method使用 StringBuilder replace() 方法

1.Using replace method使用替換方法

string.replace(char oldChar, char newChar)

public class Test {
    public static void main(String[] args) {
        String para = "Java is a beautiful language";
        String replace = para.replace('e', 'o');
        System.out.println(replace);

    }
}

Java is a boautiful languago

2.Using Charsequence使用字符序列

string.replace(char oldChar, char newChar)

public class Test {
    public static void main(String[] args) {
        String para = "Java is a beautiful language";
        String replace = para.replace("Java", "python");
        System.out.println(replace);

    }
}

python is a beautiful language

3. Replace the Substring with an empty string用空字符串替換子字符串

string.replace(char oldChar, empty char)

public class Test {
    public static void main(String[] args) {
        String para = "Java is a beautiful language";
        String replace = para.replace("beautiful", "");
        System.out.println(replace);

    }
}

Java is a  language

4.Using String’s replaceFirst method使用 String 的 replaceFirst 方法

string.replaceFirst(No. of digit, new digit)

只有字符串的前兩位數(shù)字會被這個數(shù)字改變;其余數(shù)字將保持不變。

public class Test {
    public static void main(String[] args) {
        String para = "John is 101 years old, and Mary is 20 years old";
        String replace = para.replaceFirst("\\d\\d\\d", "20");
        System.out.println(replace);

    }
}

John is 20 years old, and Mary is 20 years old

5.replaceFirst() method

string.replace(No. of digit, empty char)

public class Test {
    public static void main(String[] args) {
        String para = "John is 101 years old, and Mary is 20 years old";
        String replace = para.replaceFirst("\\d\\d\\d", "");
        System.out.println(replace);

    }
}

John is  years old, and Mary is 20 years old

6 Using replaceAll method 使用 replaceAll 方法

String replaceAll(No. of digit, int new number)

public class Test {
    public static void main(String[] args) {
        String para = "John is 101 years old, and Mary is 20 years old";
        String replace = para.replaceAll("\\d\\d\\d", "30");
        System.out.println(replace);

    }
}

John is 30 years old, and Mary is 20 years old

7.replaceAll() method

string.replace(No. of digit, empty char)

public class Test {
    public static void main(String[] args) {
        String para = "John is 10 years old, and Mary is 20 years old";
        String replace = para.replaceAll("\\d\\d", "");
        System.out.println(replace);

    }
}

John is  years old, and Mary is  years old

8.Using String Builder’s delete() method使用 String Builder 的 delete() 方法

public StringBuilder delete(int start,int end)

為了在字符串中添加和刪除字符,StringBuilder 包含一個可修改的字符序列。 一個初始容量為 16 個字符的字符串構(gòu)建器由空的 StringBuilder 函數(shù) Object() { [native code] } 創(chuàng)建,如果內(nèi)部緩沖區(qū)溢出,則會自動創(chuàng)建一個更大的字符串構(gòu)建器。 要從字符串中刪除的子字符串的開始和結(jié)束指定為 delete() 函數(shù)的第一個和第二個 int 參數(shù)。 最后一個索引是獨占的,因為它從第二個參數(shù)中減去一個,但起始索引是包含在內(nèi)的。

public class Test {
    public static void main(String[] args) {
        String para = "John is 10 years old, and Mary is 20 years old";
        StringBuilder stringBuilder = new StringBuilder(para);
        StringBuilder builder = stringBuilder.delete(7, 19);

        System.out.println(builder);
    }
}

John isd, and Mary is 20 years old

9.Using StringBuilder replace() Method使用 StringBuilder replace() 方法

string.replace(int start, int end, char newChar)

replace() 函數(shù)和 delete() 方法之間的唯一區(qū)別是第三個參數(shù),它用于替換已從字符串中刪除的字符。 如果需要替換的字符串很大,則會增加大小以容納字符串的長度。

函數(shù) toString() { [native code] }() 函數(shù)可用于在該方法返回 StringBuilder 后打印更新的字符串。文章來源地址http://www.zghlxwxcb.cn/news/detail-587677.html

public class Test {
    public static void main(String[] args) {
        String para = "John is 10 years old, and Mary is 20 years old";
        StringBuilder stringBuilder = new StringBuilder(para);
        StringBuilder builder = stringBuilder.replace(7, 19," 12 years ol");

        System.out.println(builder);
    }
}

John is 12 years old, and Mary is 20 years old

到了這里,關(guān)于Java 字符串中刪除子字符串的9種方法詳細內(nèi)容(remove substring from String)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • JavaScript刪除字符串最后一個字符的三種方法

    JavaScript刪除字符串最后一個字符的三種方法 在JavaScript中,我們經(jīng)常需要操作字符串。有時候,我們可能需要刪除字符串的最后一個字符。下面將介紹三種常見的方法來實現(xiàn)這個目標。 方法一:使用 slice 函數(shù) slice 函數(shù)是JavaScript中一個常用的字符串方法,它可以返回一個新的

    2024年02月08日
    瀏覽(23)
  • Java:JSONObject.toJSONString輸出字符串內(nèi)容處理

    提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 JSONObject可以將Java對象轉(zhuǎn)換成JSON流輸出,進行數(shù)據(jù)顯示或者數(shù)據(jù)交互的時候很好用。 但是JSONObject.toJSONString輸出的字符串往往有以下幾個格式問題: 1.Java中的 String 類型輸出的時候含有雙引號(“”)

    2024年02月03日
    瀏覽(18)
  • Java獲取字符串中逗號前的內(nèi)容

    可以使用Java的 indexOf 和 substring 方法來獲取字符串中逗號前的內(nèi)容。 首先,使用 indexOf 方法找到逗號在字符串中的位置,然后使用 substring 方法截取字符串中從開頭到逗號位置之前的內(nèi)容。 例如,假設(shè)你有一個字符串 str ,它的值為 \\\"apple,banana,orange\\\",要獲取逗號前的內(nèi)容(也

    2024年02月16日
    瀏覽(29)
  • R語言dplyr包select函數(shù)刪除dataframe數(shù)據(jù)中包含指定字符串內(nèi)容的數(shù)據(jù)列(drop columns in dataframe)

    參考鏈接 我有一個數(shù)據(jù)框,想刪除列名包含“Pval”的列 大功告成。

    2024年02月12日
    瀏覽(21)
  • Python高頻面試題——如何在字符串中刪除指定字符,掌握strip()、replace()和re.sub ()正確使用方法!

    Python高頻面試題——如何在字符串中刪除指定字符,掌握strip()、replace()和re.sub ()正確使用方法!

    關(guān)于python刪除字符串是面試python測試開發(fā)工程師的一個經(jīng)典問題。問題很簡單,但是一下子就能測試出來被面試者是否能夠熟練的進行python相關(guān)編碼工作! 對于有些臨時抱佛腳的同學來講,一看刪除,很自然就說用remove 、del相關(guān)方法,聽到這里,就知道面試者根本不知道這

    2024年02月08日
    瀏覽(30)
  • java 處理常量字符串過長 & springboot 項目讀取 resouces 文件夾下的文件內(nèi)容

    java 處理常量字符串過長 & springboot 項目讀取 resouces 文件夾下的文件內(nèi)容

    項目里面有一長串的加密字符串(最長的萬多個字符),需要拼接作為參數(shù)發(fā)送給第三方。 如果我們使用 枚舉 定義的話,idea 編譯的時候就會出現(xiàn)編譯報錯 網(wǎng)上還有一個說法,說是編譯器問題,修改 idea 工具的編譯為 eclipse 即可。 但是結(jié)果我仍然不滿意,所以我決定把他放在

    2023年04月13日
    瀏覽(92)
  • Java截取字符串方法

    主要有以下幾種方法: ? ??1、通過subString()方法來進行字符串截?。ㄗ畛S茫?? ? 2、通過StringUtils(apache的)提供的方法或者StrUtil(hutool的) ? ? 3、split()+正則表達式來進行截取 1、通過subString()方法來進行字符串截取,返回字符串中的子字符串,在java中有兩種用法 ? ?

    2024年02月16日
    瀏覽(26)
  • Java Base64字符串與String字符串互轉(zhuǎn)方法

    Java Base64字符串與String字符串互轉(zhuǎn)方法

    在使用String轉(zhuǎn)Base64和Base64轉(zhuǎn)String上有點小問題,特此記錄。 結(jié)果: 也是跟上面差不多的思路,將Base64轉(zhuǎn)為byte數(shù)組,再轉(zhuǎn)為String

    2024年02月15日
    瀏覽(32)
  • Java中截取字符串方法

    1、通過subString()方法來進行字符串截取,返回字符串中的子字符串,在java中有兩種用法 2.通過StringUtils提供的方法進行截取 以上就是常用的java截取字符串方法。

    2024年02月17日
    瀏覽(32)
  • java 分割字符串(多種方法)

    [toc] 1、String#split 可以根據(jù)給定的分隔符或正則表達式將一個字符串分割成多個部分 2、String#substring 一般情況我們都是用于截取字符串使用的,這里我們也是可以用來處理字符串的分割,只要循環(huán)就行 3、Pattern類 Pattern 類通常來說,我們是用于處理正則表達式,做一些match使

    2024年02月12日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包