Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 函数,该函数删除所有代码注释_C#_Algorithm_Function_Comments - Fatal编程技术网

C# 函数,该函数删除所有代码注释

C# 函数,该函数删除所有代码注释,c#,algorithm,function,comments,C#,Algorithm,Function,Comments,我需要做一个函数,删除文本(代码)中的所有注释。我的代码几乎完成了,但如果注释从文件的第一行开始,它就不起作用了。它表示索引超出范围,我尝试将的循环从1开始,然后将if更改为(text[I]='/'&&text[I-1]='/'),但它不起作用。 任何关于如何修复或改进代码的建议,因为它看起来很奇怪 public void RemoveComments(string text) { for (int i = 0; i < text.Length; i

我需要做一个函数,删除文本(代码)中的所有注释。我的代码几乎完成了,但如果注释从文件的第一行开始,它就不起作用了。它表示索引超出范围,我尝试将
循环从1开始,然后将
if
更改为
(text[I]='/'&&text[I-1]='/')
,但它不起作用。 任何关于如何修复或改进代码的建议,因为它看起来很奇怪

public void RemoveComments(string text)
        {
            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '/' && text[i + 1] == '/')
                {
                    text = text.Remove(i, 2);
                    for (int j = i; j < text.Length; j++)
                    {
                        if (text[j] != '\n')
                        {
                            text = text.Remove(j, 1);
                            j--;
                        }
                        else if (text[j] == '\n')
                        {
                            text = text.Remove(j, 1);
                            j--;
                            while (text[j] == ' ')
                            {
                                text = text.Remove(j, 1);
                                j--;
                            }
                            i = j;
                            break;
                        }
                    }
                }

                else if (text[i] == '/' && text[i + 1] == '*')
                {
                    text = text.Remove(i, 2);
                    for (int j = i; j < text.Length; j++)
                    {
                        if (text[j] != '*' && text[j + 1] != '/')
                        {
                            text = text.Remove(j, 1);
                            j--;
                        }

                        else if (text[j] == '*' && text[j + 1] == '/')
                        {
                            text = text.Remove(j, 2);
                            j = j - 2;
                            while (text[j] == ' ')
                            {
                                text = text.Remove(j, 1);
                                j--;
                                if (text[j] == '\n')
                                {
                                    text = text.Remove(j, 1);
                                    j--;
                                }
                            }
                            i = j;
                            break;
                        }

                    }
                }
            }
            Console.WriteLine(text);
        }
Test.txt文件

//int a;
int c; //int d;
Console.Write/*Line*/("Hhehehe");
if(1>0)
/*ConsoleWriteLine("Yes")*/
//Nooo

您有一个基于text.Length的循环

for(int i=0;i 但在循环内部,您正在缩短文本。在某一点上,它与原始文本一样小。长度和索引I guiess的用完看起来像是C代码文件。因此,你可以使用的权力。只需将代码文件解析为语法树,然后与访问者一起访问该树,该访问者将跳过注释:

var code = File.ReadAllText("Code.cs");
SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
var root = (CompilationUnitSyntax)tree.GetRoot();
var codeWithoutComments = new CommentsRemover().Visit(root).ToString();
Console.WriteLine(codeWithoutComments);
参观者:

class CommentsRemover : CSharpSyntaxRewriter
{
    public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
    {
        switch(trivia.Kind())
        {
            case SyntaxKind.SingleLineCommentTrivia:
            case SyntaxKind.MultiLineCommentTrivia:
                return default;
            default:
                return trivia;                 
        }            
    }
}
示例代码文件:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp
{
    /* Sample
       Multiline Comment */
    class Program
    {
        static void Main(string[] args)
        {
            // Comment
            Console.Write/*Line*/("Hello, World!"); // Print greeting
            /*ConsoleWriteLine("Yes")*/
        }
    }
}
输出:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp
{

    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Hello, World!");

        }
    }
}

注意:如您所见,从除了注释之外没有任何内容的行中删除注释后,您将得到空行。您可以再创建一个访问者来删除空行。也考虑删除XML注释。

可能的副本,我认为你错过了块注释的例子。你调试了代码吗?哪一行代码引发此异常?它删除了大代码中的所有注释,但我在代码的第一行添加了一条注释,它崩溃了。很抱歉,不能这么简单,您必须实现一个解析器:如果您有这样的代码:
string demo=“///”在C#for循环中,它总是在继续之前检查条件,因此这不是问题哇,谢谢,这对我来说是一种有趣的新学习方式!:o
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp
{

    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Hello, World!");

        }
    }
}