Java 字符串中删除子字符串的9种方法详细内容(remove substring from String)

总结:

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)

只有字符串的前两位数字会被这个数字改变;其余数字将保持不变。

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 个字符的字符串构建器由空的 StringBuilder 函数 Object() { [native code] } 创建,如果内部缓冲区溢出,则会自动创建一个更大的字符串构建器。 要从字符串中删除的子字符串的开始和结束指定为 delete() 函数的第一个和第二个 int 参数。 最后一个索引是独占的,因为它从第二个参数中减去一个,但起始索引是包含在内的。

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() 函数和 delete() 方法之间的唯一区别是第三个参数,它用于替换已从字符串中删除的字符。 如果需要替换的字符串很大,则会增加大小以容纳字符串的长度。

函数 toString() { [native code] }() 函数可用于在该方法返回 StringBuilder 后打印更新的字符串。

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
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值