C# strings and char arrays
I'm a C/C++ guy trying to learn C# and I'm converting some of my C code into C# for learning.
In the following snippet of code, the char array temp[] gets initialized as nulls '\0'. If I turn the array into a string object using the string constructor, the nulls become char data instead of the terminator I'd expect. Subsequent string operations don't go as planned...
Code:
char[] temp = new char[30]; // initialized as nulls '\0'
int z;
for(z=0; z<10; z++) temp[z] = '0'; // temp should be "0000000000"
string strout = new string(temp); // create string from array of 0's
strout += "12345"; // should now be "000000000012345"
However this is what strout comes up as: "0000000000\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\012345"
How do I make those nulls get treated as a string terminator?
Thanks
Re: C# strings and char arrays
So, first of all don't use this pattern (null-terminated char arrays) unless there is a compelling need to (like communicating with a server that expects it or something).
However, there are two ways to do this:
The first (and easiest, I think) is to call .Trim(char) on the string like this:
Code:
char[] temp = new char[30]; // initialized as nulls '\0'
for(int z=0; z<10; z++)
temp[z] = '0'; // temp should be "0000000000"
string strout = new string(temp); // create string from array of 0's
strout = strout.TrimEnd('\0'); //Remove any trailing null characters from the string
strout += "12345"; // should now be "000000000012345"
Note though that in C#, strings are immutable. So calling strout = strout.TrimEnd('\0'); is actually giving you an entirely different string
Alternatively, you could calculate the index of the null-terminator first and pass it as a parameter to the string constructor like this:
Code:
char[] temp = new char[30]; // initialized as nulls '\0'
for(int z=0; z<10; z++)
temp[z] = '0'; // temp should be "0000000000"
int nullIndex = Array.IndexOf<char>(temp, '\0'); //Find the null terminator
//In case there there is no null terminator, consider the whole array to be useable data
if( nullIndex < 0 )
nullIndex = temp.Length;
string strout = new string(temp, 0, nullIndex); //Three parameters: the array, the start index, the length
strout += "12345"; // should now be "000000000012345"
Console.WriteLine(strout);
Here is the relevant MSDN link: http://msdn.microsoft.com/en-us/library/ms131424.aspx
Make sense?
Good luck with C#. Your C++ background should make it easy to learn the language. Hopefully you'll find that things are a little more convenient in C# simply because you can avoid a lot of low-level management with fairly expressive syntax.
Re: C# strings and char arrays
Thanks very much for the reply. Oddly, the TrimEnd method doesn't seem to work. The string remains unchanged (the copy, that is) still retaining the '\0' characters on the end. I'm pretty sure I have it right..
Code:
char[] temp = new char[30]; // initialized as nulls '\0'
int z;
for(z=0; z<10; z++) temp[z] = '0'; // temp should be "0000000000"
string strout = new string(temp); // create string from array of 0's
strout.TrimEnd('\0'); // doesn't remove the '\0' null chars
strout += "12345"; // strout is still "0000000000\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\012345"
Your second solution does work however, thanks. But I'd like to understand why the nulls are considered as part of the string object by the append += yet they won't trim.
Thanks
Re: C# strings and char arrays
Try it this way.
[code]
string strout = new string(temp).TrimEnd('\0'); // create string from array of 0's
strout += "12345";
[\code]
Or this
[code]
strout = strout.TrimEnd('\0'); // doesn't remove the '\0' null chars
strout += "12345";
[\code]
The "Trim" doesn't wok in place since it creates a new string.
Re: C# strings and char arrays
That worked. Thanks very much.