使用C#在富文本框中的每个新行周围包装字符串

时间:2014-01-18 07:47:22

标签: c# winforms append richtextbox

我的C#Winforms应用程序中有2个富文本框,名为richtextbox1richtextbox2我还有一个名为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);
    }

}

3 个答案:

答案 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 + "')");

        }