I have the following program, What i am trying to do is join a list of names I have from a 2 text files,In the first text file i have First names, in the secont last names, I am trying to join them. Further then that I am trying to make a list of all combinations of the files ie, First names are mark and mike, last names are wilson and watson, I want to make mike watson and mike wilson
TextReader sr = new StreamReader(textBox1.Text);
TextReader sr2 = new StreamReader(textBox2.Text);
string contents = sr.ReadToEnd();
string contents2 = sr2.ReadToEnd();
string[] myArray = { contents };
string[] myArray2 = { contents2 };
foreach (string element1 in myArray)
{
foreach (string element2 in myArray2)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(element1);
sb.Append(" " + element2);
Debug.WriteLine(sb.ToString());
string str = sb.ToString();
Debug.WriteLine(str);
//MessageBox.Show(sb.ToString);
}
}
The end result should be , Mike Smith from file1 : mike, file2 : smith
thanks for the help
Using a comination of the above answers I came up with this answer, the '\r' when splitting the text files was critical as the text was read from windows 2003 (I am not sure on win 7 or 2008 what the results would be) Using the inbuilt array was good but I got completly different results when trying to run it from the filesystem, adding the '\r' fixed that Please see this post
In C#, what's the difference between \n and \r\n?
TextReader sr = new StreamReader(textBox1.Text);
TextReader sr2 = new StreamReader(textBox2.Text);
string contents = sr.ReadToEnd();
string contents2 = sr2.ReadToEnd();
string[] firstNames = contents.Split(new Char[] { '\r','\n',' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
string[] lastNames = contents2.Split(new Char[] { '\r','\n',' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
var fullnames =
from fn in firstNames
from ln in lastNames
select new { Fullname = fn + " " + ln };
foreach (var person in fullnames)
{
Debug.WriteLine(person.Fullname);
//MessageBox.Show(person.Fullname);
}