English 中文(简体)
Java tag 标签代号
原标题:Java Text tag replacer code

我认为,这似乎有局限性......这相当于我sat倒并开始打字。

不管怎么说,我的任务是撰写一种方法,在“强”中找到“主角”,并适当取代它们,从而产生结果。 职能声明将考虑如下内容:

public String doStuff(String source, Map<Integer, String> replacementData)

想法是,每一“标签”都将是四舍五入的“配对、加固、加固”和“特”类(因此,切除是“......<<[0-9]+>>)。 一旦在<代码>源上碰到这些标签之一,标签内所含的立体一就应当从标签中提取,标记应当由<代码>replacementData.get(I)替换。

无论如何,我要有效地这样做。 我不敢肯定如何做到这一点。 我对“模式”和“匹配”进行了研究,我认为,这是正确的路线。 我倾向于不使用Sting.index。 这与这一点一样,因为其效率不高(即没有效率)。

感谢大家的建议!

最佳回答

The way to do it with Regex Pattern and Matcher is to capture the integer in what is known as a group and then use that to replace. The other interesting part of your problem is the step by step replacement. The sequence of steps that you need to do are described in the javadoc for appendReplacement method

例:

String str = "abc<<1>>def<<2>>ghi";
java.util.Map<Integer,String> replacementMap = new java.util.HashMap<Integer,String>() {
    {
        put(1," ONE ");
        put(2," TWO ");
    }
};
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("<<([0-9]+)>>");
java.util.regex.Matcher matcher = pattern.matcher(str);
int start = 0;
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
    matcher.appendReplacement(sb,replacementMap.get(Integer.parseInt(matcher.group(1))));
}
matcher.appendTail(sb);
System.out.println(sb.toString());

产出:abc ONE def TWO ghi

问题回答

执行:

public String doStuff(String source, Map<Integer, String> replacementData){

    for( Map.Entry<Integer,String> entry : replacementData.entrySet() )
         source = source.replace("<<"+entry.getKey().toString()+">>", entry.getValue() );

    return source;
}

这对我来说,没有太低的效率。 提高其效率的唯一途径是设法在座标上填满一封信件,但这实际上需要改写<代码>String.replace(.

考虑到将投入的工作量,我只想把这种优化归入某类工作,只有当简介者表明这是真正的瓶颈。

The Matcher-based method by Puneet is better since it only passes over the string once.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签