string.Compare woes ??

  • Thread starter Thread starter Ramiro Calderon Romero
  • Start date Start date
R

Ramiro Calderon Romero

Hi list,

I don't know if I'm doing something wrong here.

Check out this code snippet:

class App
{
static void Main()
{
string a = "the fellowship of the ring";
string b = "The two towers";

int n = string.Compare(a,b,true);
int m = string.Compare(a,b,false);
Console.WriteLine("n={0}, m={1}",n,m);

}
}


I would expect n = -1 (I'm ignoring case here) and m = 1 (because 'T' <
't'). However I'm getting n = m = -1;

Am I wrong or it's just a weird non-intuitive behavior in the
String::Compare method?

Regards,

Ramiro
 
Hi,

What you are looking for is string.CompareOrdinal().

By default the Compare() method performs a culture specific comparison, so
the ordering of upper and lower case will be culture specific, to perform
culture insensitive string compares you can pass Culture.InvariantCulture to
the Compare() method.

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top