string equality

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

I read a list of name/value pairs from a remote (pocket pc + RAPI) registry
into a string array (3rd party wrapper for rapi registry objects).
As I check through each set, I test each name against "Completed" and will
skip the code if the test passes.
Unfortunately, the string returned to me in a watch shows as
"Completed\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
(\0s ad nauseum) and this doesn't match "Completed" so the test fails.
Any idea what's going on here please?

string[] Imports = regkey.GetValueNames();

for (int nCount = Imports.GetLowerBound(0); nCount <=
Imports.GetUpperBound(0); nCount++)

{

FileName = (string)regkey.GetValue(Imports[nCount],"");

if (FileName == "")continue;

//if (Imports[nCount] == "Completed") continue;

if (Imports[nCount].Equals("Completed")) continue;

WriteRemote(FileName);

}
 
Claire,

It looks like whatever your wrapper is, it is overallocating memory, and
assuming that the string will be compared up to the first null character in
the string.

What you want to do is find the index of the first null character, and
then trim off the rest. You can do this easily with the TrimEnd method on
the string class. You can do it like this:

// Trim the null characters off.
string trimmed = Imports[nCount].TrimEnd(new char[] { '\0' });

This should work.

Hope this helps.
 
Hello,

Can you just trim the end, and remove all the \0s?

Something like:

string a = "Completed\0\0\0\0\0\0";
a = a.TrimEnd('\0');

You think that would work for you?
 
Hi Claire

You can Trim('\0') the string before testing it. That will strip away any occurances of \0.

string str = "Completed";
foreach(string s in Imports)
{
if(str.Equals(s.Trim('\0')))
continue;
WriteRemote
}

Btw, in C# the lower bound of an array is always 0, so

for(int nCount = 0; nCount <= ; Imports.Length - 1; nCount++)
{
FileName = (string)regkey.GetValue(Imports[nCount],"");

if (FileName == "")
continue;

if ((Imports[nCount].Trim('\0')).Equals("Completed"))
continue;

WriteRemote(FileName);
}
 
Claire said:
Unfortunately, the string returned to me in a watch shows as
"Completed\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
(\0s ad nauseum) and this doesn't match "Completed" so the test fails.
Any idea what's going on here please?

No, but if you can live with it, the easy solution is to test against
"startsWith("Completed") in stead of equality.
 
Back
Top