albert
albert

Reputation: 1571

Compare Url case sensitive

Is there any class-method to test if two urls that differ in casing are the same?

these are the same :

  1. www.mysite.com
  2. Www.MYsite.COm

these are NOT the same :

  1. www.youtube.com/v=AAAABBBB
  2. www.youtube.com/v=aaaaBBBB

EDIT I dont think Uri class is enough

these two are the same links

  1. stackoverflow.com/questions
  2. stackoverflow.com/QUESTIONS

Upvotes: 0

Views: 737

Answers (2)

delrocco
delrocco

Reputation: 495

Note that www.youtube.com/v=ObgtZwwiKqg is an incorrect URL. Correct URL contains query symbol, e.g. www.youtube.com/watch?v=ObgtZwwiKqg.

How about ignore path up to query and only compare the query params? If your URLs have the query ? in them, then you can strip everything up to query. If not, you could at least strip domain with UriPartial.Authority.

For example:

Uri a = new Uri("http://www.google.com/subdirectory?v=aaBB");
Uri b = new Uri("http://www.Google.com/SUBdirectory?v=AAbb");

string aParams = a.ToString().Replace(a.GetLeftPart(UriPartial.Path), String.Empty);
string bParams = b.ToString().Replace(b.GetLeftPart(UriPartial.Path), String.Empty);
if (aParams.Equals(bParams)) // with case
{
    // they are equal
}

Upvotes: 1

Steve
Steve

Reputation: 216313

Need to use the Uri class, and check also the AbsolutePath property

string url1 = "http://www.youtube.com/v=AAAABBBB";
string url2 = "http://www.youtube.com/v=aaaaBBBB";

Uri u1 = new Uri(url1);
Uri u2 = new Uri(url2);

if(string.Compare(u1.Host, u2.Host, StringComparison.CurrentCultureIgnoreCase) == 0)
{
    if(u1.AbsolutePath == u2.AbsolutePath)
        Console.WriteLine("Equals");
    else
        Console.WriteLine("Not equal path");
}
else
    Console.WriteLine("Not equal host");

Upvotes: 0

Related Questions