读书人

怎么用String.replaceFirst函数替换“

发布时间: 2012-02-15 12:09:43 作者: rapoo

如何用String.replaceFirst函数替换“?”
String str = "where db between ? and ? ";
str.replaceFirst( "\\? ", "1234 ");
str.replaceFirst( "\\? ", "5678 ");

希望得到的结果是:str == "where db between 1234 and 5678 ";

不知怎么回事,怎么搞也搞不定啊。。。

大虾帮我!谢谢

该函数定义如下:
public String replaceFirst(String regex,
String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的第一个子字符串。

[解决办法]
String str = "where db between ? and ? ";
str = str.replaceFirst( "\\? ", "1234 ");
str = str.replaceFirst( "\\? ", "5678 ");
System.out.println(str);

读书人网 >J2SE开发

热点推荐