包含不匹配连字符的字符串比较

发布于 2024-08-26 02:44:31 字数 1051 浏览 3 评论 0原文

我在 url 重写模块中有一个方法,如下所示

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

   return x.Contains(y);
  }

但是,对于以下值,它不会返回 true:

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

但如果我用原始字符串编写单元测试,如下所示,它确实返回 true

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

可能有什么区别? 编码? 文化? 已尝试删除 ToLowerInvarient(),但这没有什么区别,

在 Match 方法中尝试了以下所有方法

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;

,但在重写模块中运行时,这些值都没有返回 true。但他们在单元测试中做到了。那么关于弦乐的东西显然是不同的,

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-09-02 02:44:31

您是否已验证两个字符串中的连字符实际上是相同的字符?我会在调试器中检查它们的字符代码,以排除字符集可能发挥的任何技巧。

Have you verified that the hyphens in the two strings are in fact the same characters? I would examine the character codes for them in the debugger to rule out any tricks that the character set may play.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文