Java字符串替换(replace)

  Java中字符串替换函数主要有三种replace、replaceFirst和replaceAll
1.replace函数
  public String replace(char oldChar,char newChar)替换字符串中所有的oldChar为newChar,返回值是字符串

String word = "wangwang";
System.out.println(word.replace("wa","mb"));//返回字符串"mbngmbng"

2.replaceFirst函数
  public String replaceFirst(String regex, String replacement)只替换第一个匹配到的值

String word = "wangwang";
System.out.println(word.replaceFirst("wa","mb"));//返回字符串"mbngwang"

你可能感兴趣的:(Java)