• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.compareTo()

 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will comparteTo() method of the String class work with internationalization(diff locale)?
- Manish
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish

As far as I know compareTo() just do character by character comparision like,
int len1 = str1.length();
int len2 = str2.length();
if ( len1 != len2 )
return len1-len2;
int i=0;
while ( i < len2 ) {
if ( str1.charAt(i) != str2.charAt(i) )
return (int)str1.charAt(i) - (int)str2.charAt(i);
}
I guess....
Here I am not sure what would happen on two strings which are not having locale en but still same as the other...
Regards
Maulin
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in compareToIgnoreCase method it is explicitly mentioned that -
Note that this method does not take locale into account, and will result in an unsatisfactory ordering for certain locales. The java.text package provides collators to allow locale-sensitive ordering.

So I was wondering if it is the same with compareTo as well.
- manish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic