C# strings and char arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: C# strings and char arrays

  1. #1
    Join Date
    Jun 2001
    Location
    USA
    Posts
    286

    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

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    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.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jun 2001
    Location
    USA
    Posts
    286

    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

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    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.

  5. #5
    Join Date
    Jun 2001
    Location
    USA
    Posts
    286

    Re: C# strings and char arrays

    That worked. Thanks very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center