我在我的Java应用程序中停留了一点。情况是我在数据库中有如下字符串
jogging(?x)
然后用户通过文本框输入类似
jogging(peter)
这意味着?x指的是彼得,必须替换它。我搜索和了解到的是,我必须使用正则表达式来查找慢跑(___)的相似性,因此空格可以是?x或alan。然后我必须找到是否在开始时有一个问号,如果没有问号,则必须通过string.replaceall函数进行替换。
请让我知道我的方向是否正确,如果不正确,我还可以使用其他什么方法。
好的,我已经做到了,也许不是最好的方法,而是最简单的方法。
String in_db;//assume it swimming(?x)
String user="swimming(duck)";
//next splitting it at the ( which will seperate the before and after ( in the string and save into array carrying first part in array[0] and rest in the array[1].
String in_dp_parts[]=in_db.split("\\(");
String user_parts[]=user.split("\\(");
// next match if the first parts of the two arrays are same then assign the rest of the first array to second one.
if(user_parts[0].equals(in_db_parts(0)))
{
in_db[1]=user[1];//assign the user provided value to in_db ?x
}
为我工作上面的代码是一个示例。根据需要进行调整。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句