1.创建一个工具类 SysDeleteText
package com.ruoyi.system.domain;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.*;
public class SysDeleteText extends HTMLEditorKit.ParserCallback {
private static SysDeleteText SysDeleteText = new SysDeleteText();
StringBuffer s;
public SysDeleteText() {
}
public void parse(String str) throws IOException {
InputStream iin = new ByteArrayInputStream(str.getBytes());
Reader in = new InputStreamReader(iin);
s = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
// the third parameter is TRUE to ignore charset directive
delegator.parse(in, this, Boolean.TRUE);
iin.close();
in.close();
}
public void handleText(char[] text, int pos) {
s.append(text);
}
public String getText() {
return s.toString();
}
public static String getContent(String str) {
try {
SysDeleteText.parse(str);
} catch (IOException e) {
e.printStackTrace();
}
return SysDeleteText.getText();
}
}
2.可以直接调用:SysDeleteText.getContent("需要处理的字符串");