Results 1 to 8 of 8
Thread: comparing strings
- 12-22-2008, 11:56 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
-
Not sure what you're asking here. The compareTo does only what you tell it to do. If you will be sorting using your own algorithm, then you'll likely use the compareTo method to help make decisions about how to sort a piece of data.
- 12-23-2008, 12:32 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
Well I assigned the values I'm comparing to two strings, named s1 and s2 respectively. I made an if statement saying:
if(s1.compareTo(s2) <= 0) {
....
}
What I'm trying to find out is whether the compareTo method for strings will return 1 if s1 is alphabetically before s2. If not, what does it do?
My program right now isn't sorting it properly so I think the error may lie there but I'm not sure.
Hope that made things clearer.
- 12-23-2008, 12:50 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 14
- Rep Power
- 0
Maybe this web can help you...
There is an explanation of the method compareTo()
Look into the API web page for the method compareTo
Currently i'm not able to post links, but..
Google -> String API
And look there for the explanation of the compareTo method....:: abretumundo.wordpress.com ::..
- 12-23-2008, 12:56 AM #5
When you say alphabetically, do you mean you want to ignore case? "A".compareTo("a") does not return 0, but "A".compareToIgnoreCase("a") does.
- 12-23-2008, 01:03 AM #6
From what I understand from the compareTo method definition, it should work, although I would recommend what Steve states, if the characters case is not an issue.
compareTo method:
This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
this.charAt(k)-anotherString.charAt(k)
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
this.length()-anotherString.length()
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-23-2008, 01:51 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 12
The String compareTo() method is essentially based on the "raw" Unicode value of the characters (BTW, to the OP, this shouldn't be a mystery -- you're allowed to look at the JDK source code, you know!).
If you want something a bit more "strictly alphabetical" for a given language, then look at the Collator class.Neil Coffey
Javamex - Java tutorials and performance info
- 12-23-2008, 05:40 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
comparing inputting strings from Joptionpane and if statement
By phil128 in forum New To JavaReplies: 2Last Post: 12-06-2008, 07:54 PM -
Problem Comparing Strings (its not what you think)
By hilather in forum New To JavaReplies: 7Last Post: 11-19-2008, 07:43 PM -
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05-21-2008, 10:03 AM -
JSTL -- Comparing two strings for equality
By trinkets in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-12-2008, 05:39 PM -
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 10:44 AM
Bookmarks