String html = "<tps>测试\n </tps>";
String replacedHtml = html.replaceAll("\\s", "").replaceAll("<tps>(.*?)</tps>", "<tps>$1</tps>");
System.out.println("Replaced HTML: " + replacedHtml);
如果cts标签外是span并且属性是color:red;font-size:15px则去掉cts标签
replaceAll("[\\r\\n]+", "")去掉换行
replaceAll("[\\r\\n]+| +","")去掉换行和空格
replaceAll("\\s", "")去掉换行空格
String html = "<div><span style='color:red;font-size:15px'><cts style='color:red;'>测试</cts>555</span></div>";
Pattern pattern = Pattern.compile("<span style=['\"]color:red;font-size:15px['\"][^>]*><cts style='color:red;'>(.*?)</cts>(.*?)</span>");
Matcher matcher = pattern.matcher(html);
StringBuilder sb = new StringBuilder(html);
int start = 0;
while (matcher.find(start)) {
// 获取匹配的内容并去除<cts>标签
String contentWithoutCtsTag = matcher.group(1) + matcher.group(2);
String spanWithContent = "<span style='color:red;font-size:15px;'>" + contentWithoutCtsTag + "</span>";
sb.replace(matcher.start(), matcher.end(), spanWithContent);
start = matcher.start() + spanWithContent.length();
}
System.out.println(sb.toString());
String str = "这是一个测试字符串\n";
if (CharMatcher.whitespace().matchesAnyOf(str) || CharMatcher.is('\n').matchesAnyOf(str)) {
System.out.println("字符串中包含空格或换行符");
} else {
System.out.println("字符串中不包含空格或换行符");
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/200897.html