从textbox2添加项目 - 多行到listbox1,其中items stsrts with textbox1 content? C# [英] Adding items from textbox2 - multiline to listbox1 , where items stsrts with textbox1 content? C#
本文介绍了从textbox2添加项目 - 多行到listbox1,其中items stsrts with textbox1 content? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
我想将代码从textBox2 - multiline 添加到listbox1,其中的项目以textBox1内容开头? C#
下面这段代码我用来将多行 textBox2中的项目添加到listBox1,它可以工作。
但是我想要listBox1中的项目,我从textBox2添加了textBox2中的内容。谢谢。
私人 void button1_Click( object sender,EventArgs e)
{
string [] s = Regex.Split(textBox2.Text, \\\\ n );
listBox1.Items.AddRange(s);
}
示例:
if 内容textBox1 = A
内容TextBox2 - multiline = something
something 1
2
3
结果 listBox1 = A
东西 1
某事 2
东西 3
我尝试了什么:
private void button1_Click( object sender,EventArgs e)
{
string [] s = Regex.Split(textBox2.Text, \r\\\
跨度>);
listBox1.Items.AddRange(s..StartsWith(textBox1.Text));
}
解决方案
尝试:
listBox1.Items .AddRange(textBox2.Lines.Select(l = > textBox1.Text + + l)。ToArray());
0)为什么使用RegEx来拆分字符串?有一个string.Split方法要快得多。
1)试试这个。
if (textbox2.Text.Length > 0 )
{
// 拆分文本in textbox2
string [] parts = textbox2.Text.Split( new char [] {' \ r',' \ n'});
// 从textbox1获取文本
string prefix = textbox1.Text;
// 如果尚未以一个
结尾,请添加一个空格prefix =(prefix.EndsWith( ))?prefix: string .Concat(前缀, );
// 重建我们的textbox2字符串
string newText = string .Empty;
for ( int i = 0 ; i < parts.Length; i ++)
{
newText = string .Concat(newtext,prefix,parts [i]);
if (i!= parts.Length -1)
{
newtext = string .Concat(newText, \\\\ nn);
}
}
textbox2.Text = newText;
}
我道歉,如果我还有一个问题。如何防止listBox1中的重复项?谢谢。
listBox1.Items.AddRange(textBox2.Lines.Select(l = > textBox1.Text + + l)。ToArray());
Hello everyone.
I would like code for adding Items from textBox2 - multiline to listbox1 , where items starts with textBox1 content? C#
This code below I use to add items from the multiline textBox2 to listBox1, and it works.
But I want to items in listBox1 whose I added from textBox2 starting with content in textBox1. Thanks.
private void button1_Click(object sender, EventArgs e)
{
string[] s = Regex.Split(textBox2.Text, "\r\n");
listBox1.Items.AddRange(s);
}
Example:
if the content textBox1 = A
content TextBox2 - multiline = something
something 1
something 2
something 3
The result in listBox1 = A something
A something 1
A something 2
A something 3
What I have tried:
private void button1_Click(object sender, EventArgs e)
{
string[] s = Regex.Split(textBox2.Text, "\r\n");
listBox1.Items.AddRange(s..StartsWith(textBox1.Text));
}
解决方案
Try:
listBox1.Items.AddRange(textBox2.Lines.Select(l => textBox1.Text + " " + l).ToArray());
0) Why are you using RegEx to split the string? There is a string.Split method that is much faster.
1) Try it this way.
if (textbox2.Text.Length > 0) { // split the text in textbox2 string[] parts = textbox2.Text.Split(new char[]{'\r','\n'}); // get the text from textbox1 string prefix = textbox1.Text; // add a space if it doesn't already end with one prefix = (prefix.EndsWith(" "))?prefix : string.Concat(prefix, " "); // rebuild our textbox2 string string newText = string.Empty; for (int i = 0; i < parts.Length; i++) { newText = string.Concat(newtext, prefix, parts[i]); if (i != parts.Length -1) { newtext = string.Concat(newText, "\r\n"); } } textbox2.Text = newText; }
I apologize, if I can have one more question. How do I prevent duplicate items in listBox1? Thank you.
listBox1.Items.AddRange(textBox2.Lines.Select(l => textBox1.Text + " " + l).ToArray());
这篇关于从textbox2添加项目 - 多行到listbox1,其中items stsrts with textbox1 content? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文