改变字符串Java的子字符串

时间:2013-10-29 12:00:40

标签: string replace expression substring

如何使用replaceAll()更改字符串的子字符串 例如使用正则表达式“1 [a-z] +”(用“X”拆分)和代码:

"this is an example 1one"

应该产生:

"this is an example 1Xone"

1 个答案:

答案 0 :(得分:1)

试试这个:

class Test {
  public static void main(String[] args) {
    String str = "this is an example 1one";
    str = str.replaceAll("1([a-z]+)", "1X$1");
    System.out.println(str);
  }
}