日期:2014-05-20  浏览次数:20789 次

急送分!!!!!!简单算法设计
template="我是${name},我今年${age}"; 
name=***,age=**;存储在map中
设计一个算法把模板字符串中对应的替换:我是***,我今年**;
我正则表达式怎么做 请指教

------解决方案--------------------
先把你要替换的内容,分别用数组存起来
然后用replaceAll(数组,要替换的内容),循环
------解决方案--------------------
Java code

public  <forname="testForm">
            <field property="max" depends="required, maxlength">
                <arg0 key="testForm.max"/>
                <arg1 key="${var:max}" name="maxlength" resource="false" />
                <var>
                    <var-name>max</var-name>
                    <var-value>8</var-value>
                </var>
            </field>
            <field property="min" depends="required,minlength">
                <arg0 key="testForm.min"/>
                <arg1 key="${var:min}" name="minlength" resource="false" />
                <var>
                    <var-name>min</var-name>
                    <var-value>4</var-value>
                </var>
            </field>
        </form>

------解决方案--------------------
如果你是不确定要替换的内容

你可以用indexOf(),我不会正则

template.indexOf("${")
自己去找位置然后再用substring()截取或者用replace替换
我喜欢这样做法,容易看得懂
------解决方案--------------------
用MessageFormat
Java code

 MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
 double[] filelimits = {0,1,2};
 String[] filepart = {"no files","one file","{0,number} files"};
 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 form.setFormatByArgumentIndex(0, fileform);

 Object[] testArgs = {new Long(12373), "MyDisk"};

 System.out.println(form.format(testArgs));

 // output, with different testArgs
 output: The disk "MyDisk" contains no files.
 output: The disk "MyDisk" contains one file.
 output: The disk "MyDisk" contains 1,273 files.