Android替换字符串不起作用

时间:2017-03-05 06:24:25

标签: java android

在下面的字符串数组我有这个标签:

html

我想用这个String[] replacementHtmlTags = { "<font color=\"red\">", "<font color=\"green\">", "<font color=\"blue\">", "</font>", "</font>", "</font>" }; 标签替换它们:

String rawParagraph = "11111 <mft:A>22222</mft:A> 33333 <mft:S> 44444 <mft:A> 555555 <mft:S> 66666 </mft:S></mft:A></mft:S><mft:R> 77777 </mft:R>"

for (int tag = 0; tag < tags.length; tag++) {
    rawParagraph.replace(tags[tag], replacementHtmlTags[tag]);
}

现在在定义目标和替换后,我的代码不起作用:

ageTypes = ageTypes.collect { it.each { it.value = (it.value instanceof String) ? it.value.trim() : it.value } }

1 个答案:

答案 0 :(得分:1)

我认为您应该遵循这种方法以获得更好的可用性和映射:

public static HashMap<String, String> keyVal;

static {
    keyVal = new HashMap<String, String>();
    keyVal.put("<mft:A>", "<font color=\\red\">");
    keyVal.put("<mft:S>", "<font color=\\green\">");
    keyVal.put("<mft:R>", "<font color=\\blue\">");
    keyVal.put("</mft:A>", "</font>");
    keyVal.put("</mft:S>", "</font>");
    keyVal.put("</mft:R>", "</font>");
}

public String replaceTag(String replace) {
    for(String key:keyVal.keySet())
        replace=replace.replaceAll(key,keyVal.get(key));
    return replace;
}