replaceAll
public String replaceAll(String regex, String replacement)
-
使用给定的 replacement 替换此字符串所有匹配给定的
正则表达式的子字符串。
调用此方法的 str.replaceAll(regex, repl) 形式与以下表达式产生的结果完全相同:
Pattern
.compile
( regex ).matcher
( str ).replaceAll
( repl )注意,在替代字符串中使用反斜杠 (\) 和美元符号 ($) 与将其视为字面值替代字符串所得的结果可能不同;请参阅
Matcher.replaceAll
。如有需要,可使用Matcher.quoteReplacement(java.lang.String)
取消这些字符的特殊含义。 -
-
-
参数:
-
regex
- 用来匹配此字符串的正则表达式 -
replacement
- 用来替换每个匹配项的字符串
返回:
- 所得 String 抛出:
-
PatternSyntaxException
- 如果正则表达式的语法无效
从以下版本开始:
- 1.4 另请参见:
/** * 使用replaceall查找字符替换为其它置顶字符 * @author Administrator * */ public class Test { /*java正则表达式特殊字符转义 点的转义:. ==> \\u002E 美元符号的转义:$ ==> \\u0024 乘方符号的转义:^ ==> \\u005E 左大括号的转义:{ ==> \\u007B 左方括号的转义:[ ==> \\u005B 左圆括号的转义:( ==> \\u0028 竖线的转义:| ==> \\u007C 右圆括号的转义:) ==> \\u0029 星号的转义:* ==> \\u002A 加号的转义:+ ==> \\u002B 问号的转义:? ==> \\u003F 反斜杠的转义:\ ==> \\u005C*/ public static void main(String[] args) { String str1 = "english*english中文#中文中文a*english中文*English中文Power!!"; String substr = "", regex = "\\u002A"; /* replaces each substring of this string that matches the given regular expression with the given replacement */ str1 = str1.replaceAll(regex, substr); System.out.println("After Replacing = " + str1); } }
-