T
Travis Ellis
I am having a problem with .net Strings.
Encoding ascii = new ASCIIEncoding();
string foo = ascii.GetString( new byte[]{ 0, 0, 0, 0 } ).Trim();
if( foo == "" )
{
Console.WriteLine("Empty"); // This never happens
}
I have noticed that when you create a string using the null characters
(byte)0 or '\u0000' that it doesn't trim the whitespace.
My example above would be the string "" but it would have a .Length of 4 so
it is not comparing right with the == operator.
If I do this:
if( foo.CompareTo("") == 0 )
{
Console.WriteLine("Empty"); //this works
}
I noticed that I can use CompareTo to compare against an empty string when I
create the string from a byte or char array that might be 0 filled.
Anybody know why this is happening?
I can do a similar thing in Java and not have the same problem:
String foo = new String( new byte[]{ 0, 0, 0, 0 } ).trim();
System.out.println(foo.length); // this prints 0 b/c the string is empty
Yet in C#.net it would print 4, but it would still appear that foo is "" by
using CompareTo
Encoding ascii = new ASCIIEncoding();
string foo = ascii.GetString( new byte[]{ 0, 0, 0, 0 } ).Trim();
if( foo == "" )
{
Console.WriteLine("Empty"); // This never happens
}
I have noticed that when you create a string using the null characters
(byte)0 or '\u0000' that it doesn't trim the whitespace.
My example above would be the string "" but it would have a .Length of 4 so
it is not comparing right with the == operator.
If I do this:
if( foo.CompareTo("") == 0 )
{
Console.WriteLine("Empty"); //this works
}
I noticed that I can use CompareTo to compare against an empty string when I
create the string from a byte or char array that might be 0 filled.
Anybody know why this is happening?
I can do a similar thing in Java and not have the same problem:
String foo = new String( new byte[]{ 0, 0, 0, 0 } ).trim();
System.out.println(foo.length); // this prints 0 b/c the string is empty
Yet in C#.net it would print 4, but it would still appear that foo is "" by
using CompareTo