String comparison / modification (Bug????)

  • Thread starter Thread starter Dim
  • Start date Start date
D

Dim

I found that C# has some buggy ways to process string across methods.
I have a class with on global string var and a method where i add / remove
from this string
Consider it a buffer... with some values and separators

class
{
private string globalvar = "";

private void manipulate (whattodo ) //substring, join, etc....
{
some manipulation on globalvar...
some join here
some substring-ing here
etc...
}
}

After couple calls to this method my global string must contain "" since it
has been processed
and all data from it have been extraced by that method and passed to another
methods, since this global var is a buffer....
I expect it to contain nothing but "" empty zero lentgh string, but it
doesn't
Apparently when u manipulate string and Join, Substring Replace etc. in it
it does nothing else bu t GROVING and groving. After a couple of this
operations i converted this string to char array and
displayed it's size. Debugger shows med an empty string , but char array
size wasmore than 1000....
and none of comparison methods (mystring == "") or (mystring.Equals("")
worked :-(

After some close examination i discovered that that char array i converted
my "empty" string to
contains nothing but 0x0 chars :-( HOW CAN IT CONTAIN null chars... ??????

I even tried to pass values through different objects (Huh? why.. dunno)
string ttt = Slices.Substring(Slices.IndexOf("|",0)+1);
Slices = "";
Slices = ttt;

This approach didn't work eighter...

Anyway to solve this and to make my comparison work when buffer string was
"" I had to add
..Replace("\0", "") to every manipulation of my buffer string...

Ex.
Slices = Slices.Substring(Slices.IndexOf("|",0)+1)
Becomes
Slices = Slices.Substring(Slices.IndexOf("|",0)+1).Replace("\0", "");



Thas seems to solve this problem, but i'm still wondering where this
behavior comes from???
Poor language engineering??? ,, Bug???,,, or am i just stupid?
 
Dim said:
I found that C# has some buggy ways to process string across methods.

I believe it's actually your understanding which is buggy, not C#.

Apparently when u manipulate string and Join, Substring Replace etc. in it
it does nothing else bu t GROVING and groving.

Um, no.
After a couple of this
operations i converted this string to char array and
displayed it's size. Debugger shows med an empty string , but char array
size wasmore than 1000....
and none of comparison methods (mystring == "") or (mystring.Equals("")
worked :-(

No, they wouldn't if your string had loads of null chars in there.
After some close examination i discovered that that char array i converted
my "empty" string to
contains nothing but 0x0 chars :-( HOW CAN IT CONTAIN null chars... ??????

It's a sequence of characters, and character 0 is a valid character.
I even tried to pass values through different objects (Huh? why.. dunno)
string ttt = Slices.Substring(Slices.IndexOf("|",0)+1);
Slices = "";
Slices = ttt;

This approach didn't work eighter...

Anyway to solve this and to make my comparison work when buffer string was
"" I had to add
.Replace("\0", "") to every manipulation of my buffer string...

Ex.
Slices = Slices.Substring(Slices.IndexOf("|",0)+1)
Becomes
Slices = Slices.Substring(Slices.IndexOf("|",0)+1).Replace("\0", "");
Thas seems to solve this problem, but i'm still wondering where this
behavior comes from???
Poor language engineering??? ,, Bug???,,, or am i just stupid?

It sounds like the strings that you're joining etc (eg Slices) have
some null characters at the end of them. I suggest you find out why,
and decide what you want to do about it.
 
No there's nothing wrong with my understanding. i've been programmer for
seven years. I think i can separate illusion from a bug.
I explicitly set ALL of the temporary vars inside the method to "" in the
beginning of the method
Incoming data contains nothing but what i expect it to contain and still
null chars are produced after
each .Substring call.
 
Btw this is not a question. I already solved it.
It's only to notify all who might have the same problem.
 
Dim said:
No there's nothing wrong with my understanding. i've been programmer for
seven years. I think i can separate illusion from a bug.

In that case it's time to demonstrate it.
I explicitly set ALL of the temporary vars inside the method to "" in the
beginning of the method

I don't see what that has to do with it.
Incoming data contains nothing but what i expect it to contain

How exactly are you checking? If you're just doing it by eye in the
debugger, that won't help you as you may not see the null chars.
and still null chars are produced after each .Substring call.

To be honest, I don't believe that. If you're still convinced there's a
bug in the .NET string routines though, you should produce a short but
complete example which demonstrates it. See
http://www.pobox.com/~skeet/csharp/complete.html
 
Back
Top