Java Strings

Hi everybody,Is there any way to sort out the list of strings with compareTo ,ignoring the case of the strings?Thanks
[138 byte] By [sidharthwa] at [2007-10-3 5:54:44]
# 1
There are toLowerCase() and toUpperCase() methods.
CaptainMorgan08a at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks I got it.toLowercase() and toUppercase() are case-Sensetive..there is compareToIgnoreCase()
sidharthwa at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 3
> Thanks I got it.> toLowercase() and toUppercase() are> case-Sensetive..there is compareToIgnoreCase()Why not just put both your strings toLower or to upper and then compare if case doesn't matter ?
Aknibbsa at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 4

> Thanks I got it.

> toLowercase() and toUppercase() are

> case-Sensetive..there is compareToIgnoreCase()

In other words, use either toUpperCase() or toLowerCase() on both Strings when you use compareTo() and you are effectively comparing them while ignoring case. i.e.

String a = "Hello is anybody in there?";

String b = "The lights are on, but nobody's home.";

if (a.toLowerCase().compareTo(b.toLowerCase()) != 0) {

System.out.println("The elevator does not go all the way to the top floor.");

}

masijade.a at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 5
ThanksI have a string list like this poke Mon, Leoparde, Stuart, gia Jowhere Mon,Leoparde,stuart and jo are lastnames ...and I want them to get compared and sortedThere is compareToIgnoreCase() but it is not helping.
sidharthwa at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 6
masijade, i dont think the elevator has left the lobby, lol.
TuringPesta at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 7
> There is compareToIgnoreCase() but it is not helping.That's strange - it works perfectly for me.
mvantuyla at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 8

> Thanks

> I have a string list like this

>

>poke Mon, Leoparde, Stuart, gia Jo

> ere Mon,Leoparde,stuart and jo are lastnames ...

> and I want them to get compared and sorted

> There is compareToIgnoreCase() but it is not helping.

How are you using it? If you are trying to use the Collections.sort or Arrays.sort methods, you need to use the version that takes a Comparator and pass in String.CASE_INSENSITIVE_ORDER.

dubwaia at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 9
I am using arrays.sort..and i am not getting any compile error while using compareToIgnoreCase()My problem is some names are like "gia Jo" (as one string) and i want just to compare and sort just "Jo"Thanks
sidharthwa at 2007-7-15 0:35:24 > top of Java-index,Java Essentials,New To Java...
# 10

> I am using arrays.sort..and i am not getting any

> compile error while using compareToIgnoreCase()

sort doesn't use that method by default. You need to use a Comparator if you are not now.

> My problem is some names are like "gia Jo" (as one

> string) and i want just to compare and sort just

> "Jo"

That's a completely different problem. You need to parse the Strings into the portions you want to compare and there's no way for anyone to tell you how without a clear understanding of what data is possible and what your desired results are.

dubwaia at 2007-7-15 0:35:25 > top of Java-index,Java Essentials,New To Java...
# 11
String class is final. so can't inherit it to define your own property.you can have a class, say Person , with fields firstname and lastname.then you can use Comparabe interface.
Thanigaivel.Ma at 2007-7-15 0:35:25 > top of Java-index,Java Essentials,New To Java...
# 12
this link would help you http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
Thanigaivel.Ma at 2007-7-15 0:35:25 > top of Java-index,Java Essentials,New To Java...
# 13
> masijade, i dont think the elevator has left the> lobby, lol.And it looks as though the lights are not even on.
masijade.a at 2007-7-15 0:35:25 > top of Java-index,Java Essentials,New To Java...