【发布时间】:2013-05-27 19:03:21
【问题描述】:
我正在寻找如下替换 java 字符串值。下面的代码不起作用。
cleanInst.replaceAll("[<i>]", "");
cleanInst.replaceAll("[</i>]", "");
cleanInst.replaceAll("[//]", "/");
cleanInst.replaceAll("[\bPhysics Dept.\b]", "Physics Department");
cleanInst.replaceAll("[\b/n\b]", ";");
cleanInst.replaceAll("[\bDEPT\b]", "The Department");
cleanInst.replaceAll("[\bDEPT.\b]", "The Department");
cleanInst.replaceAll("[\bThe Dept.\b]", "The Department");
cleanInst.replaceAll("[\bthe dept.\b]", "The Department");
cleanInst.replaceAll("[\bThe Dept\b]", "The Department");
cleanInst.replaceAll("[\bthe dept\b]", "The Department");
cleanInst.replaceAll("[\bDept.\b]", "The Department");
cleanInst.replaceAll("[\bdept.\b]", "The Department");
cleanInst.replaceAll("[\bdept\b]", "The Department");
实现上述替换的最简单方法是什么?
【问题讨论】:
-
不工作是什么意思?
-
删除方括号(
[和])。这些用于字符类。如果其他方法不起作用,则需要更具体。 -
字符串是不可变的。
-
和忽略大小写修饰符适用于很多
dept替换 -
正如@SLaks 所指出的:字符串是不可变的。如果您不将
String.replaceAll()的返回值存储在某处,您的代码将什么也不做。现在,您的代码对返回值没有任何作用。
标签: java regex string replaceall