Click here to Skip to main content
11,415,164 members (73,403 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
System.IO.File.WriteAllLines(@"\\Path\Output.txt", ArrayName);
this works fine for 1d array but doesn't work for 2d array

please help
Posted 28-Dec-12 4:18am
Edited 28-Dec-12 4:25am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Member 9715673 at 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?
ProgramFOX at 29-Dec-12 3:39am
   
For a string[][] or a string[,]?
Member 9715673 at 7-Jan-13 5:50am
   
string[,]
ProgramFOX at 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 at 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
ProgramFOX at 18-Jan-13 11:22am
   
It's working fine for me.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,200
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web03 | 2.8.150427.4 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100