public static void stripComments(Scanner input) {
while (input.hasNextLine()) {
String noComments = input.nextLine().replace("//", "").replace("*/", "").replace("/*", "");
System.out.println(noComments);
}
}
到目前为止代码。它剥离了注释和符号。但它不会删除任何注释掉的内容或注释之间的内容。一个例子是:
import java.util.*;
My program - NEEDS TO BE GONE
by Suzy Student - NEEDS TO BE GON
public class Program {
public static void main(String[] args) {
System.out.println("Hello, world!"); a println - NEEDS TO BE GONE
}
public static (Hello there - NEEDS TO BE GONE) void foo() {
System.out.println("Goodbye!"); comment here - NEEDS TO BE GONE
}
}
在评论的所有内容消失后应该是什么样子:
import java.util.*;
public class Program {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
public static void foo() {
System.out.println("Goodbye!");
}
}
有什么想法吗?如果以某种方式我可以在评论符号等之后删除文本。