# 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

참조사이트

Normal ConnectionString : (work for xls files)

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=YES;\""

Office 2007 ConnectionString : (work for xlsx files)

Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=YES;\""

 

private void UseOlebForLoadExcel()
{
	string filePath = "C:\\99.download\\샘플.xlsx";

	int nOutputRow = 0;
	string sSheetName = null;
	string sConnection = null;
	DataTable dtTablesList = default(DataTable);
	OleDbCommand oleExcelCommand = default(OleDbCommand);
	OleDbDataReader oleExcelReader = default(OleDbDataReader);
	OleDbConnection oleExcelConnection = default(OleDbConnection);


	sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ filePath + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\"";

	oleExcelConnection = new OleDbConnection(sConnection);
	oleExcelConnection.Open();

	dtTablesList = oleExcelConnection.GetSchema("Tables");

	if (dtTablesList.Rows.Count > 0)
	{
		SheetName = dtTablesList.Rows[0]["TABLE_NAME"].ToString();
	}

	dtTablesList.Clear();
	dtTablesList.Dispose();

	if (!string.IsNullOrEmpty(sSheetName))
	{
		oleExcelCommand = oleExcelConnection.CreateCommand();
		oleExcelCommand.CommandText = "Select * From [" + sSheetName + "]";
		oleExcelCommand.CommandType = CommandType.Text;
		oleExcelReader = oleExcelCommand.ExecuteReader();
		nOutputRow = 0;

		while (oleExcelReader.Read())
		{
			// reader.GetDouble(0);
			var index = grid.Rows.Add();
			grid.Rows[index].Cells["filePath"].Value = oleExcelReader.GetString(0);
			grid.Rows[index].Cells["fileName"].Value = oleExcelReader.GetString(0);
			grid.Rows[index].Cells["lineNo"].Value = oleExcelReader.GetString(0);
			grid.Rows[index].Cells["fileCnts"].Value = oleExcelReader.GetString(0);
		}
		oleExcelReader.Close();
	}
	oleExcelConnection.Close();
}
728x90

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

ChartFX Help  (0) 2020.01.19
remove comments with C#  (0) 2019.10.14
c# 관련사이트  (0) 2019.10.14
Highlight Textbox  (0) 2019.10.14
Visual Studio Installer 를 이용한 배포(Deployment)  (0) 2019.10.14

1.Sample Code

2.Component

 

 

728x90

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

ChartFX Help  (0) 2020.01.19
remove comments with C#  (0) 2019.10.14
excel upload faster  (0) 2019.10.14
Highlight Textbox  (0) 2019.10.14
Visual Studio Installer 를 이용한 배포(Deployment)  (0) 2019.10.14

 

728x90

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

ChartFX Help  (0) 2020.01.19
remove comments with C#  (0) 2019.10.14
excel upload faster  (0) 2019.10.14
c# 관련사이트  (0) 2019.10.14
Visual Studio Installer 를 이용한 배포(Deployment)  (0) 2019.10.14

+ Recent posts