我希望做一个函数,它可以获取一个字符串,并在其中包含内联注释时将其移除。
public class sample {
public static void main(String[] args) {
String code = "/**THIS IS SAMPLE CODE */ public class TestFormatter{public static void main(String[] args){int i =2; String s= \"name\";\\u give give change the values System.out.println(\"Hello World\");//sample}}";
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
TextEdit textEdit = codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT, code1, 0, code1.length(), 0,
null);
IDocument doc = new Document(code1);
try {
textEdit.apply(doc);
System.out.println(doc.get());
} catch (MalformedTreeException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
我在textEdit.apply(doc)
处遇到了空指针异常。这是因为它不接受注释。
你能告诉我从字符串中删除注释的最佳方法是什么吗?(请不要提供太高级的解决方案。)