Re: String Bug or somethig I missunderstood?

Curtis Koppang <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.general
Message-ID <[email protected]>
Are you a C programmer? ;-)  A char-zero is not used as the null terminator
in a string.  Most string implementations (besides C) use some sort of
length descriptor to determine the length of the string.  To reinforce this
point, check out the constructor for string - one takes an array of chars.
If you were a Java or VB programmer this would seem perfectly normal to you.

Later,
curt

----- Original Message -----
From: "Miguel Ángel Chacón" <[email protected]>
To: <[email protected]>
Sent: Wednesday, June 12, 2002 11:42 AM
Subject: [DOTNET] String Bug or somethig I missunderstood?


Just execute this lines of code and see the results. If you build a
string from a char array like this:
{'B','U','G',0,0,0,0}, the resulting string have a length of 8, and if
you build a new string adding some words to the first string and you
write it you´ll see 'blanks' for the '0' char, like this:
"BUG     NEW BUG". My expected result is that the length of the first
string to be 3 (no?) and when I add something the result should be
"BUGNEWBUG". Am I wrong?

using System;
using System.IO;

namespace StringBugTest
{
        class Class1
        {
                [STAThread]
                static void Main(string[] args)
                {
                        byte[] bytes = new byte[8];
                        bytes.SetValue((byte)66,0);
                        bytes.SetValue((byte)85,1);
                        bytes.SetValue((byte)71,2);
                        bytes.SetValue((byte)0,3);
                        bytes.SetValue((byte)0,4);
                        bytes.SetValue((byte)0,5);
                        bytes.SetValue((byte)0,6);
                        bytes.SetValue((byte)0,7);
                        MemoryStream ms=new MemoryStream(bytes);
                        BinaryReader bReader=new BinaryReader(ms);
                        char[] cadena=bReader.ReadChars(8);
                        string bug=new string(cadena);
                        Console.WriteLine(bug);
                        int a=bug.Length;
                        Console.WriteLine(a);
                        bug+="NEW BUG";
                        Console.WriteLine(bug);
                }
        }
}

Thanks for your attention.
Miguel Ángel Chacón Espín.
Telecinco. Spain.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.