比较2个网址的最佳方式

heg*_*hin 9 c# string-comparison

我想比较2个网址.什么是最好的方法呢?

条件:1)它应该排除http方案.2)'foo.com/a/b'和'foo.com/a'应该匹配.

Pat*_*son 28

您应该使用Uri.Compare方法.

以下是将两个URI与不同方案进行比较的示例.

public static void Test()
{
    Uri uri1 = new Uri("http://www.foo.com/baz?bar=1");
    Uri uri2 = new Uri("https://www.foo.com/BAZ?bar=1");

    var result = Uri.Compare(uri1, uri2, 
        UriComponents.Host | UriComponents.PathAndQuery, 
        UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase);

    Debug.Assert(result == 0);
}
Run Code Online (Sandbox Code Playgroud)


asa*_*rob 9

使用c#URI类来表示您的URI

然后使用uri.compare函数