当前位置: 代码迷 >> J2SE >> 字符串替换解决方案
  详细解决方案

字符串替换解决方案

热度:39   发布时间:2016-04-23 21:45:42.0
字符串替换
如下面的一个字符串,想把内容ABC替换成hello.
<string name="STR_ABC">ABC</string>
最后结果:
<string name="STR_ABC">hello</string>
请问怎么更好更快地实现替换!
string 替换

------解决方案--------------------
public class tihuan {
public static void main(String[] args){
String s="<string name=\"STR_ABC\">ABC</string>";
System.out.println(s.replaceAll("(<.*>)ABC(<.*>)","$1hello$2" ));
}
}


------解决方案--------------------
方法太多
		String s = "<string name=\"STR_ABC\">ABC</string>";
System.out.println(s.replace(">ABC<", ">hello<"));

------解决方案--------------------
没事再来一个

public class tihuan {
public static void main(String[] args) {
String s = "<string name=\"STR_ABC\">ABC</string>";
System.out.println(s.replaceAll("(?<=>)\\w+(?=<)", "hello"));
}
}
  相关解决方案