Question

I'm having a bit of an issue. Lets say I have 2 text boxes, one on the left with this content:

Win
Lose
Hello
Goodbye

And one on the right, with this information:

One
Two
Three
Four

Now, on button press, I want to combine these two text boxes with colon delimitation, so it would output like this:

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

Any idea how I can accomplish this? Nothing I have tried thus far has worked. This is my current code, sorry. I'm not trying to have you do my work for me, I'm just rather confused:

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();
Was it helpful?

Solution 2

The code below demonstrates one way of splitting strings and then concatenating them. I misunderstood the question at first. :)

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;
    }
}

OTHER TIPS

Here's a different approach that might work for you.

You can generate a couple string arrays by splitting on the new line character.

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

And then use LINQ's Zip() method to combine them into a new list. The first element in each list is combined, then the second elements in each, and so on...

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

In order for this to work, both TextBoxes must have the same number of lines. If they differ, then Zip won't work.

Well if this was a winforms app you could take advantage of the Lines property to do the following.

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();
textbox1.Text.Split("\n").Zip(texbox2.Text.Split("\n"),(s1,s2)=>s1+":"+s2).Aggregate("",(a,s)=>a+s+"\n")

Split method converts the string on behalf which it was call, to array of strings, by splitting it with character in parameter (new line in this case).

At this movement we have to arrays of lines from textbox1 and textbox2.

Now we use Zip method of any IEnumerable (this is an extension method as the Aggregate method is). The outcome of Zip method is a IEnumerable that contains elements that are merge from both IEnumerables that we mentioned using function passed in the parameters, in this case it is (s1,s2)=>s1+":"+s2.

At this moment we have some IEnumerable having elements as merged lines from both textboxes. What we need to do now is to merge them into one string with Aggregate function. It a function that construct result starting with first parameter and for each element taking the result of last step and returning new value that is some kind of aggregation of the previous result and current element

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;
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top