Java 字符串中删除子字符串的9种方法详细内容(remove substring from String) 您所在的位置:网站首页 删除字符串s中的所有t字符串 Java 字符串中删除子字符串的9种方法详细内容(remove substring from String)

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

2024-06-06 09:10| 来源: 网络整理| 查看: 265

总结:

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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有