简体   繁体   English

如何合并两个文本?

[英]How to combine two texts?

I have a Single line text box and Multiline text box, and want to include a word into the Single line text box with the words in Multiline text box per line 我有一个Single line文本框和一个Multiline文本框,并且想在单行文本框中包含一个单词,每行Multiline行文本框中的单词

Like this : 像这样 :

Single line text: "Hello"(I have to use variables)<br> 单行文字: "Hello"(I have to use variables)<br>

Multiline words: 多行字:

<br>
1998<br>
1999<br>
2000

Expected results: 预期成绩:


Hello1998 你好1998
Hello1999 你好1999
Hello2000 你好2000
Pls Help me 请帮助我

I use the below code, but it is not working just with the Single line text box and I have to manipulate by both text boxes: 我使用下面的代码,但它不能仅与“ Single line文本框一起使用,并且必须通过两个文本框进行操作:

string left = string.Format(add.Text , Environment.NewLine);
        string right = string.Format(textBox1.Text, Environment.NewLine);
        string[] leftSplit = left.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
        string[] rightSplit = right.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

        string output = "";
        if (leftSplit.Length == rightSplit.Length)
        {
            for (int i = 0; i < leftSplit.Length; i++)
            {
                output += leftSplit[i] + ":" + rightSplit[i] + Environment.NewLine;
            }
        }
        result.Text = output;

Could you please advise me on the right approach? 您能给我建议正确的方法吗?

TextBox.GetLineText(int) will help you: TextBox.GetLineText(int)将帮助您:

var singlelineText = singlelineTextBox.Text;
var composedLines = new List<string>();
for (var i = 0; i < multilineineTextBox.LineCount; i++)
{
  composedLines.Add(singlelineText + multilineineTextBox.GetLineText(i));
}

result.Text = string.Join(EnvironmentNewline, composedLines);

If you have single line only one word , then no need split it into an array. 如果单行只有一个单词 ,则无需将其拆分为一个数组。

Lets consider it as a string left = "Hello"; 让我们将其视为string left = "Hello";

and textbox1 contains multiline words ie 和textbox1包含多行单词,即

string right = string.Format(textBox1.Text, Environment.NewLine); // right variable contains 1998 \n 1999 \n 2000

Then you can try below approach 然后您可以尝试以下方法

var concatString = right.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(x => left + x);
string result = string.Join(Environment.NewLine , concatString);

.Net Fiddle .Net小提琴

Output : 输出:

Hello1998
Hello1999
Hello2000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM