包含不匹配连字符的字符串比较
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否已验证两个字符串中的连字符实际上是相同的字符?我会在调试器中检查它们的字符代码,以排除字符集可能发挥的任何技巧。
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.