我正在尝试在C#中创建一个例程,用于对添加到多行文本框的列表进行排序.完成后,可以选择删除所有空行.有人能告诉我怎么做这个吗?这是我到目前为止所做的,但是当我选择框并点击排序时它根本不起作用:
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);
}
Run Code Online (Sandbox Code Playgroud)
Ry-*_*Ry- 22
如果要拆分字符串'\n',sortBox1则不包含包含的字符串\n.我会用String.IsNullOrWhiteSpace,但是:
sortBox1.RemoveAll(string.IsNullOrWhiteSpace);
Run Code Online (Sandbox Code Playgroud)
你忘了排序:
sortBox1.Sort();
Run Code Online (Sandbox Code Playgroud)
空白行不是"\r\n",这是一个换行符.空行是空字符串:
sortBox1.RemoveAll(item => item.Length == 0);
Run Code Online (Sandbox Code Playgroud)
您还可以在拆分字符串时删除空行:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16387 次 |
| 最近记录: |