# remove comments with C#

  1. Remove comments
  2. Maintain line number
using System;
using System.Text;
using System.Windows.Forms;

namespace WFRemoveComment
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRemoveComment_Click(object sender, EventArgs e)
        {
            string s = System.IO.File.ReadAllText(@"C:\workspace_cs\WindowsFormsApp1\WindowsFormsApp1\Form1.cs");
        
            this.rtxText1.Text = s;
            this.rtxText2.Text = removeComment(s);
        }

        public static string removeCommentStyleJava(string s)
        {
            char lineCommentChar1 = '/';
            char lineCommentChar2 = '/';

            return removeComment(s, lineCommentChar1, lineCommentChar2);
        }

        public static string removeCommentStyleSql(string s)
        {
            char lineCommentChar1 = '-';
            char lineCommentChar2 = '-';

            return removeComment(s, lineCommentChar1, lineCommentChar2);
        }

        public static string removeComment(string s, char lineCommentChar1 = '/', char lineCommentChar2 = '/')
        {
            StringBuilder endResult = new StringBuilder(1000);

            const int outsideComment = 0;
            const int insideLineComment = 1;
            const int insideblockComment = 2;
            
            int currentState = outsideComment;
            if(s == null)
            {
                return endResult.ToString();
            }
            
            char c = (char) 0;
            char c2 = (char)0;
            for(int i = 0, cnt = s.Length; i < cnt; i++)
            {
                c = s[i];
                switch (currentState)
                {
                    case outsideComment:
                        if (c == lineCommentChar1 && (i+1) < cnt)
                        {
                            c2 = s[++i]; // 다음건 read
                            if (c2 == lineCommentChar2)
                                currentState = insideLineComment;
                            else if (c2 == '*')
                                currentState = insideblockComment;
                            else
                                endResult.Append(c).Append(c2);
                        }
                        else
                            endResult.Append(c);
                        break;
                    case insideLineComment:
                        if (c == '\r')
                            endResult.Append(c);

                        if (c == '\n')
                        {
                            currentState = outsideComment;
                            endResult.Append(c);
                        }
                        break;
                    case insideblockComment:
                        if (c == '\r' || c == '\n')
                            endResult.Append(c);

                        if (c == '*' && (i+1) < cnt)
                        {
                            c2 = s[++i]; // 다음건 read
                            if (c2 == '/')
                            {
                                currentState = outsideComment;
                                break;
                            }
                        }
                        break;
                } // end of switch
            } // end of for

            return endResult.ToString();
        }
    }
}

 

 

end.

728x90

'Programming Language > C#' 카테고리의 다른 글

[펌] Correlation of two arrays in C#  (0) 2020.02.06
ChartFX Help  (0) 2020.01.19
excel upload faster  (0) 2019.10.14
c# 관련사이트  (0) 2019.10.14
Highlight Textbox  (0) 2019.10.14

+ Recent posts