總結(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ù),它用于替換已從字符串中刪除的字符。 如果需要替換的字符串很大,則會增加大小以容納字符串的長度。文章來源:http://www.zghlxwxcb.cn/news/detail-587677.html
函數(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)!