I need to do a function which deletes all comments from the text(code). My code is almost finished, but it doesn't work if comment starts in the first line of the file. It says index out of bounds, I tried changing for
loops to start from 1 and then if
to(text[i] == '/' && text[i - 1] == '/')
but it doesn't work.
Any suggestion how can I fix that or improve my code because it looks weird.
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);
}
EDIT: Now I did many experiments and I found that the problem is with(in // loop) I needed this loop this to fix some small aligment problems:
while (text[j] == ' ')
{
text = text.Remove(j, 1);
j--;
}
Test.txt file.
//int a;
int c; //int d;
Console.Write/*Line*/("Hhehehe");
if(1>0)
/*ConsoleWriteLine("Yes")*/
//Nooo
Looks like you have C# code files. Thus you can use the power of Roslyn. Simply parse code file into syntax tree and then visit that tree with visitor which skips comments:
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);
Visitor:
class CommentsRemover : CSharpSyntaxRewriter
{
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
{
switch(trivia.Kind())
{
case SyntaxKind.SingleLineCommentTrivia:
case SyntaxKind.MultiLineCommentTrivia:
return default; // new SyntaxTrivia() // if C# <= 7.0
default:
return trivia;
}
}
}
Sample code file:
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")*/
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello, World!");
}
}
}
Notes: As you can see, after removing comments from the lines which had nothing except comment, you get empty lines. You can create one more visitor to remove empty lines. Also consider to remove XML comments as well.