我的C#Winforms应用程序中有2个富文本框,名为richtextbox1
和richtextbox2
我还有一个名为button1
的按钮。这个想法是当最终用户将值列表粘贴到richtextbox1
时,例如,
C1
C2
C3
C4
richtextbox2
的结果应该是:(这是我想要的帮助)
IN ('C1','C2','C3', 'C4')
这是我到目前为止所做的:
private void button1_Click(object sender, EventArgs e)
{
string strValues;
strValues = richTextBox1.Text;
//MessageBox.Show(strValues);
string strInStatement;
strInStatement = richTextBox2.Text;
List<string> idsList = new List<string>() { strValues };
string whereClause = string.Join(",", idsList).ToString();
richTextBox1.Lines = idsList.ToArray();
foreach (string value in idsList)
{
MessageBox.Show(value);
}
}
答案 0 :(得分:1)
你可以试试这个:
private void button1_Click(object sender, EventArgs e)
{
var textInEachLine = richTextBox1.Text.Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
string whereClause = string.Join("', '", textInEachLine).ToString();
MessageBox.Show(" IN ( '" + whereClause + "')");
}
此代码将删除空行(如果有),并用单引号在每行中换行文本。
答案 1 :(得分:1)
试试这个:
private void button1_Click(object sender, EventArgs e)
{
string whereClause = String.Join("','", richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.None));
richtextbox2.Text = (" IN ( '" + whereClause + "' )");
}
答案 2 :(得分:0)
试用此代码
private void button1_Click(object sender, EventArgs e)
{
string whereClause = String.Join("','", richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.None));
MessageBox.Show(" IN ( '" + whereClause + "')");
}