String Comparison containing hyphens not matching

Posted by Christo Fur on Stack Overflow See other posts from Stack Overflow or by Christo Fur
Published on 2010-03-16T12:51:14Z Indexed on 2010/03/16 12:56 UTC
Read the original article Hit count: 368

Filed under:
|

I have a method in a url rewriting module that looks like this

public bool Match(Uri url)
  {
      string x = url.PathAndQuery.ToLowerInvariant();
      string y = RuleData.ToLowerInvariant();

   return x.Contains(y);
  }

However, it is not returning true for the following values:

x = "/xx09-02-09xx"; y = "09-02-09";

but if I write a unit test with the raw strings, like below, it does return true

[Test]
        public void Contains()
        {
            string x = "/xx09-02-09xx";
            string y = "09-02-09";
            Assert.IsTrue(x.Contains(y)); // this returns true
        }

What could be the difference? The encoding? The culture? Have tried removing the ToLowerInvarient(), but that makes no difference

have tried all the following in the Match method

bool contains = x.Contains(y);
bool contains1 = x.IndexOf(y) != -1;
bool contains2 = x.IndexOf(y, StringComparison.OrdinalIgnoreCase) != -1;
bool contains3 = x.IndexOf(y, StringComparison.InvariantCultureIgnoreCase) != -1;
bool contains4 = x.IndexOf(y, StringComparison.CurrentCultureIgnoreCase) != -1;

but none return true for those values, when run in the rewrite module. But they do in the unit test. So something about the strings is clearly different

any ideas?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about string-comparison