strings in .Net

  • Thread starter Thread starter Miki Watts
  • Start date Start date
M

Miki Watts

Is there someting in .Net that is equivalent to the std::string ? i.e. can
contain several null characters and dynamic in size.

The string object in .Net is Null terminated, so it can't function like
that, and byte arrays are fixed size.

Miki
 
James Curran said:
Miki Watts said:
Is there something in .Net that is equivalent to the std::string ? i.e. can
contain several null characters and dynamic in size.

There's char[] & byte[]

but are they dynamic in size? i haven't found a way to append to a byte[]
....

Miki
 
Is there someting in .Net that is equivalent to the std::string ? i.e. can
contain several null characters and dynamic in size.

The string object in .Net is Null terminated, so it can't function like
that, and byte arrays are fixed size.

Miki

It is not exactly std::string, but the System.Text.StringBuilder class
may do what you need.

HTH,
Tim
 
Tim Smelser said:
It is not exactly std::string, but the System.Text.StringBuilder class
may do what you need.

Hmm... so you suggest to build the string in the StringBuilder and create a
byte[] instead of a string from it?

Miki
 
Tim Smelser said:
It is not exactly std::string, but the System.Text.StringBuilder class
may do what you need.

Hmm... so you suggest to build the string in the StringBuilder and create a
byte[] instead of a string from it?

Miki

If you want the end result to be a byte array, you could use the
GetBytes() method from the appropriate encoding class in System.Text on
the String returned from the StringBuilder. Or if all you want is a
sizable array, the System.Collections.ArrayList provides that. The main
problem there is that you would be dealing with Object types which may
be less performant.

Tim
 
Tim Smelser said:
Tim Smelser said:
On Fri, 20 Feb 2004 18:40:00 +0200, Miki Watts wrote:

Is there someting in .Net that is equivalent to the std::string ? i.e. can
contain several null characters and dynamic in size.

The string object in .Net is Null terminated, so it can't function like
that, and byte arrays are fixed size.

Miki

It is not exactly std::string, but the System.Text.StringBuilder class
may do what you need.

Hmm... so you suggest to build the string in the StringBuilder and create a
byte[] instead of a string from it?

Miki

If you want the end result to be a byte array, you could use the
GetBytes() method from the appropriate encoding class in System.Text on
the String returned from the StringBuilder.

Great, that solved another problem that i was having :)
Or if all you want is a
sizable array, the System.Collections.ArrayList provides that. The main
problem there is that you would be dealing with Object types which may
be less performant.

something like doing std::vector<byte> in STL , you mean? guess i'll have to
resort to that...
 
Miki Watts said:
Is there someting in .Net that is equivalent to the std::string ? i.e. can
contain several null characters and dynamic in size.

The string object in .Net is Null terminated, so it can't function like
that, and byte arrays are fixed size.

Although strings in .NET *are* null-terminated internally, from the
point of view of entirely managed code, they're not, and can certainly
contain several null characters.

Strings themselves are immutable, but you can build one from another,
or use StringBuilder.
 
Jon Skeet said:
Although strings in .NET *are* null-terminated internally, from the
point of view of entirely managed code, they're not, and can certainly
contain several null characters.

I guess i haven't got to that part yet... could you show me an example?

Miki
 
Miki Watts said:
I guess i haven't got to that part yet... could you show me an example?

Sure:

using System;

public class Test
{
static void Main()
{
string x = "hello\0there\0lots\0of\0nulls";
Console.WriteLine (x.Length);
}
}

(\0 is the escape sequence for character 0).
 
Jon Skeet said:
Sure:

using System;

public class Test
{
static void Main()
{
string x = "hello\0there\0lots\0of\0nulls";
Console.WriteLine (x.Length);
}
}

(\0 is the escape sequence for character 0).

ok, thanks. The debugger was giving me a length of 5 on that string, but
checking the length through a message box showed me that it was 25 indeed.
Guess i won't be needing the byte collection array class i wrote :)

Miki
 
ok, thanks. The debugger was giving me a length of 5 on that string, but
checking the length through a message box showed me that it was 25 indeed.

Are you using .Net 2002? That was a known bug that's been fixed in the
newer version.

Chris R.
 
Back
Top