当前位置: 代码迷 >> J2SE >> 怎么用String.replaceFirst函数替换“?”
  详细解决方案

怎么用String.replaceFirst函数替换“?”

热度:376   发布时间:2016-04-24 17:03:37.0
如何用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);
  相关解决方案