用字符串替换。。当然 你也可以用正则表达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)

解决方案 »

  1.   

    谢谢不过我的意思是:
    已知:
    pattern = "${a}_${b}-${cd}_${p}"
    str = "test_text-comp_int"解析出:
    ${a} = test
    ${b} = text
    ${cd} = comp
    ${p} = int而且,在实际应用时,我是不知道在模式中出现的变量是a(${a})还是N1(${N1})或者是别的什么
    我就是希望在不可预知变量名以及模式的情况,根据用户提供的模式和字符串
    从字符串中提取出用户在模式中定义的变量在字符串中对应的那段字符串应该如何做呢?
      

  2.   

    用StringTokenizer,以${做为分割符,产生一个Enumeration,再对每一个枚举元素进行判断,如果最后一位不是}就把最后一位提出来做为匹配用的操作符,这样又可以产生一个String[]或是ArrayList,接下来对这个String[]操作,把每一个元素在实际字符串中的位置记录下来,用substring就可以得到结果了。一点意见,仅供参考。(好像是烦了一点)
      

  3.   

    To Eraserpro(工作好难找啊!租房子好贵啊!...):
    你的思路我大致明白,确实有些繁琐,我去试试看
    To Kick_hotdog(khd):
    在下对正则表达式了解不多,请问能否告诉我我这种情况应该如何用java编写正则表达式?谢谢 ^_^
      

  4.   

    http://www.fanqiang.com/a4/b5/20011113/0808001561.html