Hi, I'm new to C#.
I have a 2-D char array as follows:
char [,] c_Line = new c_Line[45,1000];
Then in the program, I will fill-in elements in this 2-D array like c_Line[x, y] = 'X', etc. At the end I would have c_Line filled with characters.
Now, I want to take the first row, i.e., c_Line[0] (I'm from C/C++, there c_Line[0] would represent the whole first row) and convert into String object.
So if I do this, it gives compilation error that it expects the 2nd component in the [].
String s_FirstRec = new String (c_Line[0].ToString());
How do I convert this 2-D char array into 45 String objects?
Please advise.
Thanks./
AlanPosted Oct 23, 2007, 11:28 AM
One thing that takes a bit of getting used to when you're moving from C/C++ to C#, is that strings and char arrays are distinct types in .NET. Also strings are immutable but char arrays are not.
However, the two are related in that you can create a string object from a char array using one of the string classes constructors and individual chars can be read from the string using its indexer property:
string s = "abcd";
char c = s[0]; // 'a'
Also memory management in .NET is done by the CLR and, whilst you can manipulate memory in C# using pointers in 'unsafe' code, it's considered bad form to do so unless you really need the extra speed.
The point I'm getting around to here is that manipulation of strings n C# is not as easy or as powerful as it is in C/C++ though it is safer and (usually) reasonably speedy.
However, having said that, there are a ton of methods in the String class and one that might be useful here is the CopyTo() method:
http://msdn2.microsoft.com/en-us/library/system.string.copyto(vs.80).aspx
JayakrishnaPosted Oct 23, 2007, 9:03 AM
Ok. One other question related to this subject.
Is there any strcpy(), strncpy() or memmove() functions in C#. I want to move strings into c_Line[0] array (different positions) without me doing a for loop to copy individual characters one-by-one. If there is strcpy or strncpy, I could easily move character string to the c_Line[0] array efficiently?
Is it possible in C#?
Thanks again.
JayakrishnaPosted Oct 22, 2007, 4:28 PM
AlanPosted Oct 22, 2007, 3:43 PM
As the jagged array is distinct from the 'ordinary' multidimensional array, the two are not interchangeable and you have to stick to the [x][y] style of indexing with the former. So, if you change that line to the following it should work fine:
c_Line[10][44] = 'A';
JayakrishnaPosted Oct 22, 2007, 3:03 PM
Alan, Here is what I found out:
It allows me to do this:
char[][] c_Line = new char [45][]; for (int i = 0; i < 45; i++){
c_Line[i] =
new char[1000];}
// initialize arrayHowever when I try to access elements like 2-D, it gives a compile error:
c_Line[10,44] =
'A';JayakrishnaPosted Oct 22, 2007, 2:57 PM
AlanPosted Oct 22, 2007, 2:55 PM
Even though it's rectangular, you'd be better here working with a 'jagged' array rather than an ordinary 2-D array:
char [][] c_Line = new c_Line[45][];
for (int i = 0; i < 45; i++)
{
c_Line[i] = new char[1000];
}
// initialize array
You could then just do:
String s_FirstRec = new String (c_Line[0]);
Otherwise, I think you'll just have to copy each row of the 2D array to a temporary single dimensional array, which can only be done element by element as the Array.Copy method doesn't work when the source and destination arrays are of different dimensions. You can then convert the temporary array to a string.