Java:从字符串中删除注释

Pop*_*oko 4 java string comments

我想做一个获取字符串的函数,如果它有内联注释,它会删除它.我知道这听起来很简单,但我想确保我这样做是正确的,例如:

private String filterString(String code) {
  // lets say code = "some code //comment inside"

  // return the string "some code" (without the comment)
}
Run Code Online (Sandbox Code Playgroud)

我想了两种方法:随意提出建议

  1. 迭代字符串并找到双内联括号并使用子字符串方法.
  2. 正则表达方式..(我不太确定回合它)

你能告诉我什么是最好的方式并告诉我应该怎么做?(请不要建议太高级的解决方案)

编辑:这可以用Scanner对象以某种方式完成吗?(我还是使用这个对象)

Loï*_*oni 7

如果您想要一个更高效的正则表达式来真正匹配所有类型的注释,请使用以下一个:

replaceAll("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)",""));
Run Code Online (Sandbox Code Playgroud)

来源:http://ostermiller.org/findcomment.html

编辑:

另一个解决方案,如果你不确定使用正则表达式是设计一个如下的小自动机:

public static String removeComments(String code){
    final int outsideComment=0;
    final int insideLineComment=1;
    final int insideblockComment=2;
    final int insideblockComment_noNewLineYet=3; // we want to have at least one new line in the result if the block is not inline.

    int currentState=outsideComment;
    String endResult="";
    Scanner s= new Scanner(code);
    s.useDelimiter("");
    while(s.hasNext()){
        String c=s.next();
        switch(currentState){
            case outsideComment: 
                if(c.equals("/") && s.hasNext()){
                    String c2=s.next();
                    if(c2.equals("/"))
                        currentState=insideLineComment;
                    else if(c2.equals("*")){
                        currentState=insideblockComment_noNewLineYet;
                    }
                    else 
                        endResult+=c+c2;
                }
                else
                    endResult+=c;
                break;
            case insideLineComment:
                if(c.equals("\n")){
                    currentState=outsideComment;
                    endResult+="\n";
                }
            break;
            case insideblockComment_noNewLineYet:
                if(c.equals("\n")){
                    endResult+="\n";
                    currentState=insideblockComment;
                }
            case insideblockComment:
                while(c.equals("*") && s.hasNext()){
                    String c2=s.next();
                    if(c2.equals("/")){
                        currentState=outsideComment;
                        break;
                    }

                }

        }
    }
    s.close();
    return endResult;   
}
Run Code Online (Sandbox Code Playgroud)

  • 正则表达式解决方案和您提供的解决方案将破坏包含字符串文字中的注释起始字符序列的源代码。 (2认同)

mak*_*aks 5

执行此操作的最佳方法是使用正则表达式.首先找到/**/注释然后删除所有//commnets.例如:

private String filterString(String code) {
  String partialFiltered = code.replaceAll("/\\*.*\\*/", "");
  String fullFiltered = partialFiltered.replaceAll("//.*(?=\\n)", "")
}
Run Code Online (Sandbox Code Playgroud)


rsp*_*rsp 2

使用正则表达式替换来查找常量子字符串之前的子字符串有点多。

您可以使用indexOf()检查注释开始的位置并substring()获取第一部分来完成此操作,例如:

String code = "some code // comment";
int    offset = code.indexOf("//");

if (-1 != offset) {
    code = code.substring(0, offset);
}
Run Code Online (Sandbox Code Playgroud)

  • 太简单了——会破坏类似这样的内容:`String url="http://www.google.com";` (4认同)