string equality

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);

}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
K

Kristofer Gafvert

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?
 
M

Morten Wennevik

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);
}
 
J

joachim lous

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top