Click here to Skip to main content
16,020,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 string arrays of same length i need them to form a 2d string array with array1 in first column and array 2 in second column to get the output i used to use:
C#
System.IO.File.WriteAllLines(@"\\Path\Output.txt", ArrayName);

this works fine for 1d array but doesn't work for 2d array

please help
Posted
Updated 28-Dec-12 3:25am
v2

HI,

It doesn't work for a 2D array, because the second parameter of the WriteAllLines funtion must be a 1D array. To write a 2D array to a file, have a look at the first answer of this question:
http://social.msdn.microsoft.com/Forums/pl-PL/csharpgeneral/thread/1091f0ca-3bff-44e9-8359-7888fa07fa71[^]
 
Share this answer
 
Comments
Member 9715673 28-Dec-12 21:47pm    
Thanks, I am able to get the output in a text file now. But the orientation is not as desired. How can I get the output in a tabular format?
Thomas Daniels 29-Dec-12 3:39am    
For a string[][] or a string[,]?
Member 9715673 7-Jan-13 5:50am    
string[,]
Thomas Daniels 7-Jan-13 12:31pm    
Try this:
StringBuilder outputBuilder = new StringBuilder();
for (int i = 0; i < ArrayName.GetLength(0); i++)
{
for (int j = 0; j < ArrayName.GetLength(1); j++)
{
outputBuilder.Append(ArrayName[i, j]);
if (j != ArrayName.GetLength(1) - 1)
{
outputBuilder.Append("\t");
}
else
{
outputBuilder.AppendLine();
}
}
}
Console.WriteLine(outputBuilder.ToString());
System.IO.File.WriteAllText("file.txt",outputBuilder.ToString());
Member 9715673 18-Jan-13 4:20am    
the entries are of variable length in both the columns
i was looking to have them arranged in a tabular manner like

Array1 Array2
==================================================
wqqwqwq | eyuyuiyuy
dsdsdsds | youyuiyuiuyiuyiuy
jkljlkjlkjlkjljl | 9089898djdidkkd
even here the output is not coming as desired :(

I'm fine even if there are no separators like the === and| used here
but i do need the second column to start from the same point in every row
currently the output is jagged like this

wqqwqwq eyuyuiyuy
dsdsdsds youyuiyuiuyiuyiuy
jkljlkjlkjlkjljl 9089898djdidkkd

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900