2

我有两个字符串如下:

String input="<tr><td>Hello world</td></tr>";
String output="<body><tr><td>Hello world</td></tr></body>";

我想比较这两个字符串,<body></body>如果输入字符串不包含标签,则需要从输出字符串中删除<body>标签。字符串可以是任意长度。

如何根据输入字符串比较和替换输出字符串? <body>标签可能类似于<body ></body > or < body>< /body>,etc.

需要找到标签并根据输入字符串替换它。

4

4 回答 4

1
static String[] combinations = new String[] {
    "<body>","</body>","< body>","< /body>","<body >"
};

for (int i = 0; i < combinations.length; i++) {
    if (!input.contains(combinations[i])) {  
        output = output.replace(combinations[i], "");    
    }    
}
于 2012-09-07T10:59:04.067 回答
1
 String resultingOutput = "";
 if( !(input.contains("<body>")) && !(input.contains("</body>")) ){
      resultingOutput = output.replace("<body>", "").replace("</body>","");
 }
于 2012-09-07T11:00:40.620 回答
-1
int compare;
if (!input.contains("<body>"))
compare = output.replaceAll("<body>","").replaceAll("</body>","").compareTo(input);
于 2012-09-07T11:03:36.123 回答
-1
public class Practice {

public static void main(String[] args) {
    String input = "<tr><td>Hello world</td></tr>";
    String output = "<body><tr><td>Hello world</td></tr></body>";
    String tag = "<body>";
    String replace = "";

    System.out.println("Output : " + validate(input, output, tag, replace));

}

private static String validate(String input, String output, String word,
        String replace) {

    if (!(input.contains(word)) && !(input.contains(word))) {
        output = output.replace(word, replace).replace(word, replace);
    }
    return output;

}}
于 2012-09-07T11:06:59.410 回答