2012-12-16 3 views
7

複数行のテキストボックスに追加されたリストをソートするルーチンをC#で作成しようとしています。これが完了すると、すべての空白行を削除するオプションがあります。誰かが私にこのことをどうやって行くのか教えてもらえますか?ここで私がこれまで持っているものですが、それは私がボックスを選択すると、全く動作し、ソートをクリックしません:C#リストから空白行を削除するには<string>?

private void button1_Click(object sender, EventArgs e) 
{ 
    char[] delimiterChars = { ',',' ',':','|','\n' }; 
    List<string> sortBox1 = new List<string>(textBox2.Text.Split(delimiterChars)); 

    if (checkBox3.Checked) //REMOVE BLANK LINES FROM LIST 
    { 
     sortBox1.RemoveAll(item => item == "\r\n"); 
    } 

    textBox3.Text = string.Join("\r\n", sortBox1); 
} 

答えて

20

あなたは'\n'に文字列を分割している場合、sortBox1が入った文字列が含まれません。 \n。私はちょうどかかわらず、String.IsNullOrWhiteSpace使用します。

sortBox1.Sort(); 

空白行は、それが改行で、"\r\n"ではありません。

sortBox1.RemoveAll(string.IsNullOrWhiteSpace); 
7

あなたが行をソートするために忘れてしまいました。空白行は、空の文字列です:

sortBox1.RemoveAll(item => item.Length == 0); 

文字列を分割するときにも空白行を削除することができます

private void button1_Click(object sender, EventArgs e) { 
    char[] delimiterChars = { ',',' ',':','|','\n' }; 

    StringSplitOptions options; 
    if (checkBox3.Checked) { 
     options = StringSplitOptions.RemoveEmptyEntries; 
    } else { 
     options = StringSplitOptions.None; 
    } 

    List<string> sortBox1 = new List<string>(textBox2.Text.Split(delimiterChars, options)); 
    sortBox1.Sort(); 
    textBox3.Text = string.Join("\r\n", sortBox1); 
} 
関連する問題