0

我有点问题。假设我有 2 个文本框,左侧的一个包含以下内容:

Win
Lose
Hello
Goodbye

右边的一个,带有以下信息:

One
Two
Three
Four

现在,在按下按钮时,我想将这两个文本框与冒号分隔符组合起来,所以它会输出如下:

Win:One
Lose:Two
Hello:Three
Goodbye:Four

知道我怎么能做到这一点吗?到目前为止,我所尝试的一切都没有奏效。这是我当前的代码,对不起。我不是想让你为我做我的工作,我只是很困惑:

string path = Directory.GetCurrentDirectory() + @"\Randomized_List.txt";
string s = "";
StringBuilder sb = new StringBuilder();
StreamReader sr1 = new StreamReader("Randomized_UserList.txt");
string line = sr1.ReadLine();
while ((s = line) != null)
{
   var lineOutput = line+":";
   Console.WriteLine(lineOutput);
   sb.Append(lineOutput);
}
sr1.Close();
Console.WriteLine();
StreamWriter sw1 = File.AppendText(path);
sw1.Write(sb);
sw1.Close();
4

5 回答 5

3

这是一种可能对您有用的不同方法。

您可以通过在换行符上拆分来生成几个字符串数组。

var tb1 = textBox1.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var tb2 = textBox2.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

然后使用 LINQ 的Zip()方法将它们组合成一个新的列表。组合每个列表中的第一个元素,然后组合每个列表中的第二个元素,依此类推...

var combined = tb1.Zip(tb2, (s1, s2) => string.Format("{0}:{1}", s1, s2));

为了使其工作,两个TextBoxes 必须具有相同的行数。如果它们不同,那么Zip将无法正常工作。

于 2014-05-05T22:21:04.880 回答
1

好吧,如果这是一个 winforms 应用程序,您可以利用 Lines 属性执行以下操作。

var tb1 = this.textBox1.Lines.Select((line, index) => new { Line = line, Index = index });
var tb2 = this.textBox2.Lines.Select((line, index) => new { Line = line, Index = index });

var q = from t1 in tb1
        join t2 in tb2 on t1.Index equals t2.Index
        select string.Format("{0}:{1}", t1.Line, t2.Line);

this.textBox3.Lines = q.ToArray();
于 2014-05-05T22:28:29.413 回答
1

下面的代码演示了一种拆分字符串然后将它们连接起来的方法。一开始我误解了这个问题。:)

string left = string.Format("Win{0}Lose{0}Hello{0}Goodbye", Environment.NewLine);
string right = string.Format("One{0}Two{0}Three{0}Four", 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;
    }
}
于 2014-05-05T22:08:52.687 回答
0
textbox1.Text.Split("\n").Zip(texbox2.Text.Split("\n"),(s1,s2)=>s1+":"+s2).Aggregate("",(a,s)=>a+s+"\n")

Split 方法通过使用参数中的字符(在本例中为新行)将字符串拆分为字符串数组。

在这个动作中,我们必须从 textbox1 和 textbox2 中提取行数组。

现在我们使用任何 IEnumerable 的 Zip 方法(这是与 Aggregate 方法一样的扩展方法)。Zip 方法的结果是一个 IEnumerable,其中包含从我们提到的使用参数中传递的函数合并的两个 IEnumerable 的元素,在本例中为 (s1,s2)=>s1+":"+s2。

此时我们有一些 IEnumerable 元素作为来自两个文本框的合并行。我们现在需要做的是使用聚合函数将它们合并为一个字符串。它是一个从第一个参数开始构造结果的函数,每个元素获取最后一步的结果并返回新值,该值是先前结果和当前元素的某种聚合

于 2014-05-05T22:31:30.427 回答
0

You can split a string by linebreaks in the following way:

string[] lines = theString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

I think you should split the content of both TextBoxes like that, and after that (if the resulting arrrays are of the same size), concatenate the corresponding (the first string from the first array with the first string form the second array, the second string from the first array with the second string from the second array, etc.) strings with a semicolon between them.

For example:

var lines1 = textBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
var lines2 = textBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string result = String.Empty;
if (lines1.Length == lines2.Length)
{
   for(int i=0; i< lines1.Length; ++i)
   {
       result += lines1[i] + ":" + lines2[i] + Environment.NewLine;
   }
}
于 2014-05-05T22:13:09.280 回答