- 使用“;”替換過字符串中的“,”。
public class Test01 {
public static void main(String[] args) {
String number = "123,456,5234,52345,63456,7456,7";
String newNumber = number.replace(",", ";");
System.out.println(newNumber);
}
}
結果:
123;456;5234;52345;63456;7456;7
- replaceAll,replace,replaceFirst的區別
public class Test01 {
public static void main(String[] args) {
//replaceAll,replace,replaceFirst的區別
String strTmp = new String("BBBBBBBYYYYYYY");
//replaceAll支持正則表達式和字符替換
strTmp = strTmp.replaceAll ("\\D", "Y");
System.out.println(strTmp);
strTmp = strTmp.replaceAll ("Y", "N");
System.out.println(strTmp);
//replace支持字符和字符串替換
strTmp = strTmp.replace("N", "C");
System.out.println(strTmp);
//只替換第一個字符
strTmp = strTmp.replaceFirst("\\D", "q");
System.out.println(strTmp);
}
}
結果: