퇴근5분전


아래 소스는 훈스게시판에 중복문자열을 찾아서 지우는 질문이 있었는데 

숫자타입만을 제거한다고 된걸 못보고 만들었다. 질문을 대충보는 버릇때문인지...

아래는 변수명을 억지로 한글로 바꾸어봤는데... 대충 대충 지었더니... 영어보다 더 보기 어렵다...

집에서 다시 고쳐보자!! 한글로 뭔가 객체지향느낌이 나도록....


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Text = "aa a bb c d e ffff c a aa bbbbbb b";
        }

        public class 중복문자제거 : IDisposable
        {
            int 중복체크문자갯수 = 0;

            int 인덱스 = 0;
            List<char> 문자히스토리 = new List<char>();
            List<string> 중복문자 = new List<string>();

            public 중복문자제거(int 문자갯수 )
            {
                중복체크문자갯수 = 문자갯수;
            }

            public string 실행(string 문자열소스)
            {
                string 반환문자열 = "";
                인덱스 = 0;
                for (int loop = 0; loop < 문자열소스.Length; loop++)
                {
                    // 검색문자 뽑기 
                    char? c = 검색문자뽑기(문자열소스);
                    if (c != null)
                    {
                        // hist와 체크
                        bool b = 문자체크(c.Value);

                        if (b == false)
                        {
                            중복문자체크(문자열소스, c.Value);
                        }
                        인덱스++;
                    }
                    if (인덱스 >= 문자열소스.Length - 1)
                    {
                        반환문자열 = 문자변환(문자열소스).Replace("  ", " ");
                      
                    }
                }
                return 반환문자열;

            }

            private string 문자변환(string 문자열소스)
            {
                foreach (string s in 중복문자)
                {
                    문자열소스 = 문자열소스.Replace(s, "");
                }
                return 문자열소스;
            }

            private void 중복문자체크(string 문자열소스, char 체크문자)
            {
                try
                {
                    string s = new string(체크문자, 중복체크문자갯수);
                    if (문자열소스.IndexOf(s, 인덱스) >= 0)
                    {
                        중복문자.Add(s);
                    }
                }
                catch
                {}
            }

            private bool 문자체크(char c)
            {
                bool ret = false;
                if (문자히스토리.Contains(c))
                {
                    ret = true;
                }
                else
                {
                    ret = false;
                    문자히스토리.Add(c);
                }
                return ret;
            }

            private char? 검색문자뽑기(string 문자열소스)
            {
                char? 뽑기문자 = null;
                try
                {
                    뽑기문자 = 문자열소스[인덱스];
                }
                catch { }
                return 뽑기문자;
            }

            #region IDisposable 멤버

            public void Dispose()
            {
                문자히스토리.Clear();
                중복문자.Clear();
                중복문자 = null;
                문자히스토리 = null;
            }

            #endregion
        }


        private void button1_Click(object sender, EventArgs e)
        {
            using (중복문자제거 cc = new 중복문자제거(2))
            {
                this.textBox1.Text = cc.실행(this.textBox1.Text);
            }
        }
    }