用字符串替换。。当然 你也可以用正则表达public static String Replace(String source, String oldString, String newString)
{
if(source == null)
{
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart;
int pos;
for(posStart = 0; (pos = source.indexOf(oldString, posStart)) >= 0; posStart = pos + lengOfold)
{
output.append(source.substring(posStart, pos));
output.append(newString);
} if(posStart < lengOfsource)
{
output.append(source.substring(posStart));
}
return output.toString();
}在这里为 Replace(str ,‘${a}’,test)
{
if(source == null)
{
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart;
int pos;
for(posStart = 0; (pos = source.indexOf(oldString, posStart)) >= 0; posStart = pos + lengOfold)
{
output.append(source.substring(posStart, pos));
output.append(newString);
} if(posStart < lengOfsource)
{
output.append(source.substring(posStart));
}
return output.toString();
}在这里为 Replace(str ,‘${a}’,test)
已知:
pattern = "${a}_${b}-${cd}_${p}"
str = "test_text-comp_int"解析出:
${a} = test
${b} = text
${cd} = comp
${p} = int而且,在实际应用时,我是不知道在模式中出现的变量是a(${a})还是N1(${N1})或者是别的什么
我就是希望在不可预知变量名以及模式的情况,根据用户提供的模式和字符串
从字符串中提取出用户在模式中定义的变量在字符串中对应的那段字符串应该如何做呢?
你的思路我大致明白,确实有些繁琐,我去试试看
To Kick_hotdog(khd):
在下对正则表达式了解不多,请问能否告诉我我这种情况应该如何用java编写正则表达式?谢谢 ^_^