简体   繁体   English

字符串格式删除后的所有内容 </table> 标签

[英]String formatting remove everything after </table> tag

I have a string of HTML that contains a table. 我有一个包含表的HTML字符串。
However in some cases there are characters after the </table> tag. 但是,在某些情况下, </table>标记后会有一些字符。
I want to remove everything after the </table> tag from the string. 我想从字符串中删除</table>标记后的所有内容。
Any ideas on how I might do this in Java? 关于如何在Java中执行此操作的任何想法?

假设str是此字符串,则可以执行以下操作:

str = str.replaceAll("</table>.*","</table>");

Use regular expressions as @Andrew Logvinov suggests. 使用@Andrew Logvinov建议的正则表达式

str.replaceAll("</table>.*","</table>");

What this means, is: take the literal String "</table>" follewed by any character . 这意味着:以任何字符后面的文字字符串"</table>"开头. repeated zero or more times * and replace it for the literal String "</table>" 重复零次或多次*并将其替换为文字字符串"</table>"

Note that this method replaces all (so it takes the first match and delete the rest of the string), so if you have multiple tables, or nested tables this won't work any good. 请注意,此方法将替换所有内容(因此它需要第一个匹配项并删除字符串的其余部分),因此,如果您有多个表或嵌套表,这将无法正常工作。 Learn to use regular expressions for a better solution. 学习使用正则表达式以获得更好的解决方案。 (Or just work with indexOf and whatever methods are in the String class) (或者只使用indexOf以及String类中的任何方法)

String endTableTag = "</table>";
String html = "<table><tr><td>Hello</td></tr></table>yadayadayada";
int index = html.lastIndexOf(endTableTag);
String cleanedup;
if (index>-1) {
    // Removes everything after the last table end tag
    cleanedup = html.substring(0, index + endTableTag.length());
} else {
    cleanedup = html;
}
System.out.println(cleanedup);
String test = "testabc</table>anothertest</table>hahhah";
test = str.substring(0, (str.lastIndexOf("</table>")+"</table>".length()));
System.out.println(str.substring(0, (str.lastIndexOf("</table>")+"</table>".length())));
System.out.println(test);

or to test it... 或进行测试...

test.replaceAll("</table>.*","</table>"); would be better! ;) 

Is good, but if you have more that 1 you might get problems! 很好,但是如果您的数量超过1,则可能会遇到问题! ;) So its better to work with "lastIndexOf()" imho! ;)因此,最好与imho的“ lastIndexOf()”一起使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM