1.replace()方法
replace() 方法用于將目標(biāo)字符串中的指定字符(串)替換成新的字符(串)
字符串.replace(String oldChar, String newChar)
public class Test1 {
public static void main(String[] args) {
String a = "1-1-1-1";
String result = a.replace("-","");
System.out.println(result);
}
}
1111
2.replaceFirst()方法
replaceFirst() 方法用于將目標(biāo)字符串中匹配某正則表達(dá)式的第一個子字符串替換成新的字符串
字符串.replaceFirst(String regex, String replacement)文章來源:http://www.zghlxwxcb.cn/news/detail-508312.html
public class Test1 {
public static void main(String[] args) {
String a = "1-1-1-1";
String result = a.replaceFirst("1","0");
System.out.println(result);
}
}
0-1-1-1
3.replaceAll()方法
replaceAll() 方法用于將目標(biāo)字符串中匹配某正則表達(dá)式的所有子字符串替換成新的字符串
字符串.replaceAll(String regex, String replacement)文章來源地址http://www.zghlxwxcb.cn/news/detail-508312.html
public class Test1 {
public static void main(String[] args) {
String a = "1-1-1-1";
String result = a.replaceAll("1","0");
System.out.println(result);
}
}
0-0-0-0
到了這里,關(guān)于Java中String字符串替換3種方法詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!