Java String replace() 字符串替换

Java String 的字符串替换主要涉及到三个方法:replace() 、replaceFirst()、replaceAll()。

字符串替换是将字符串中存在的某一片段替换为另一个字符串片段。

replace() 方法

String replace()方法是 Java 中的重载方法。它有两个变体。

  • public String replace(char oldChar, char newChar) 返回一个字符串oldChar,该字符串是用替换此字符串中所有出现的newChar。
  • public String replace(CharSequence target, CharSequence replacement) 返回一个字符串target,该字符串是用replacement子字符串替换此字符串中所有出现的子字符串而产生的。

实际上,就是一个替换字符,一个替换字符串。

替换字符示例代码

把小写的 ‘h’ 换成大写的 ‘H’:

public static void main(String[] args) {
    String originalString = "hello, my name is henry";
    String newString = originalString.replace('h', 'H');

    System.out.println(originalString);
    System.out.println(newString);
}
hello, my name is henry
Hello, my name is Henry
执行结果

把中文的逗号替换成英文的逗号:

public static void main(String[] args) {
    String originalString = "枯藤,老树,昏鸦";
    String newString = originalString.replace(',', ',');

    System.out.println(originalString);
    System.out.println(newString);
}
枯藤,老树,昏鸦
枯藤,老树,昏鸦
执行结果

替换字符串

将短语中的部分单词修正:

public static void main(String[] args) {
    String originalString = "Whatever is worthwhile doing is worthwhile doing well";
    String newString = originalString.replace("worthwhile", "worth");

    System.out.println(originalString);
    System.out.println(newString);
}
Whatever is worthwhile doing is worthwhile doing well
Whatever is worth doing is worth doing well
执行结果

入参均不能为空 null

任何一个参数为 null,都会抛出NullPointerException。

public static void main(String[] args) {
    String originalString = "Whatever is worthwhile doing is worthwhile doing well";
    String newString = originalString.replace("worthwhile", null);
}
Exception in thread “main” java.lang.NullPointerException
at java.lang.String.replace(String.java:2240)
执行结果

replaceFirst() 替换首个匹配项

String replaceFirst()方法替换正则表达式第一次匹配的内容为给定字符串。入参均不能为空,否则会抛出NullPointerException。

String replaceFirst() 方法

在 Java 中,replaceFirst() 方法定义如下:

public String replaceFirst(String regex, String replacement) {
    return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}

其中第一个入参为正则表达式,第二个参数为需要替换的内容,而 replace() 方法的两个参数均为常规字符串,不能识别正则表达式。

replaceFirst() 方法示例

替换第一次出现的 ‘worthwhile’为指定值:

public static void main(String[] args) {
    String originalString = "Whatever is worthwhile doing is worthwhile doing well";
    String newString = originalString.replaceFirst("worthwhile", "worth");

    System.out.println(originalString);
    System.out.println(newString);
}
Whatever is worthwhile doing is worthwhile doing well
Whatever is worth doing is worthwhile doing well
执行结果

替换正则表达式:

public static void main(String[] args) {
    String originalString = "Whatever is worthwhile doing is worthwhile doing well";
    String newString = originalString.replaceFirst("worth.*", "ok");

    System.out.println(originalString);
    System.out.println(newString);
}
Whatever is worthwhile doing is worthwhile doing well
Whatever is ok
执行结果

replaceAll() 方法

replace() 方法不能接受正则表达式,replaceAll()可以认为是接受正则表达式的 replace()

replaceFirst() 方法只能替换第一次匹配正则的部分,replaceAll()是完全替换。

### String replaceAll() 方法

在 Java 中,replaceAll() 方法定义如下:

“`java
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
“`

其中第一个入参为正则表达式,第二个参数为需要替换的内容。正则表达式必须语法正确,否则抛出PatternSyntaxException。第二个参数不能为空 null,否则抛出NullPointerException。

替换所有出现的”worthwhile”为指定字符串:

“`java
public static void main(String[] args) {
String originalString = "Whatever is worthwhile doing is worthwhile doing well";
String newString = originalString.replaceAll("worthwhile", "worth");

<pre><code>System.out.println(originalString);
System.out.println(newString);
</code></pre>

}

“`

Whatever is worthwhile doing is worthwhile doing well
Whatever is worth doing is worth doing well
执行结果

替换所有的空格为中划线:

public static void main(String[] args) {
    String originalString = "Whatever is worthwhile doing is worthwhile doing well";
    String newString = originalString.replaceAll("\\s", "-");

    System.out.println(originalString);
    System.out.println(newString);
}
Whatever is worthwhile doing is worthwhile doing well
Whatever-is-worthwhile-doing-is-worthwhile-doing-well
执行结果
标签: