mco
mco

Reputation: 422

c# substring not found although the substring exists

I have strings which look like

string1 = "~01301~^~DATA1,DATA2 DATA3~^15.87^717^0.85^81.11^2.11^0.06^0"

string2 = "~01341~^~DATA3,DATA2 DATA1 DATA4~^15.87^717^0.85^81.11^2.11^0.06^0"

string3 = "~01347~^~DATA1 DATA2,DATA3~^15.87^717^0.85^81.11^2.11^0.06^0"

and so on.

Out of these strings, I need to find which strings contain let's say "DATA1" substring. In C#, contains - indexOf - lastIndexOf methods cannot find DATA1 in string1 but they all find DATA1 in string2 and string3.

What could be the reason for this? First DATA1 is surrounded with tilde and comma but I guess those shouldn't affect or am I wrong?

EDIT: The relevant part of the code is trivial, that's why I didn't post it. But still here is the relevant part of the code:

while((line = isoFileReader.ReadLine())!=null)
{
    if (line.IndexOf(input)!=-1)
    {
        matchList.Add(line);
    }
}

or

while((line = isoFileReader.ReadLine())!=null)
{
    if (line.Contains(input))
    {
        matchList.Add(line);
    }
}

Upvotes: 0

Views: 1739

Answers (1)

NominSim
NominSim

Reputation: 8511

Most likely an issue when you make the call. string1.Contains("DATA1"); will return true for the string you specified.

Contains is case-sensitive, so perhaps you've accidentally typed the wrong case of one of the letters, or added a space before/after.

Upvotes: 2

Related Questions