• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compareTo() Method of the String Class

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
It is not clear to me as to what the method
" int compareTo(String otherString) " does to a String, in the String Class. It states it does a Lexical Comparison, what is this ???
Does it simply compare the length of 2 strings and return a value less that 0 if the String passed in is shorter in length than the String from which you call the method, and a value greater than 0 if the passed String is greater ???
maybe I dont understand the word Lexical !?!
All Code example greatly appreciated.
Thanks in advance.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear but>
Actually a string is less than another if it comes before the other. A string greater than another if it comes ofter the other in dictionary.

REMEMMBER:
int compare(String str)
the invoking string is less than str -- less than zero return
the invoking string is greater than str -- greater than zero return
two string are equal -- zero return
HOPE NOW IT IS CLEAR.
love to all
waseem
 
waseem akhtar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear but>
Actually a string is less than another if it comes before the other. A string greater than another if it comes ofter the other in dictionary.

REMEMMBER:
int compare(String str)
the invoking string is less than str -- less than zero return
the invoking string is greater than str -- greater than zero return
two string are equal -- zero return
HOPE NOW IT IS CLEAR.
love to all
waseem
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks Waseem.
The values less than 0 and greater than 0, are they a fixed value or can they vary ??
Thanks, in advance.
 
waseem akhtar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
zahid,
values will return is cases:
less than zero
& greater than zero
will very .
BUT MY QUESTION TO ALL READER'S WHO THESE VALUES ARE CALCULATED.
REPLY will be apreciated.
waseem
love to all
 
waseem akhtar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear zahid,
values will vary. (IN BOTH CASES YOU MENSSIONED)
zero will return only when string are equal.
BUT I DON't KNOW IN WHAT MANNER THE VALUES WILL RETURN IF STRING'S ARE less than OR greater than THE INVOKING STRING.
love to all
waseem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value returned from compareTo method will depend upon the Strings compared.For example :
class Test{
public static void main(String args[]){
System.out.println(args[0].compareTo(args[1]));}}
Different cases for the different value at the command line arguement
case 1 : "abc" , "abcdef"
The returned value will be the difference of the length of abc and that of abcdef. Output 3-6=-3
Because in this case "abc" is a substring of "abcdef"
case 2 : "abcdef " , "ab"
Output 6-2=4
case 3 : "abhdef" , abdef
Here in this case noone is substring of another.Hence the check is done whether the first 2 letters of the both the strings differ and like wise it goes on till the first ocurring dissimilsrity.In the above case the the 3rd letter of both the strings differ.
Output : The place of h - the place of d =4
case 4 : "abcdef","ad"
Output : b-d=-2
I hope u r clear...


 
waseem akhtar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i am really sorry for dupliction.
Actually when i was submiting reply due to some reason i thought
that you haven't got it .
sorry again.
love to all
waseem
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
Thanks Debasish. Your answer was quite clear.
Zahid.
 
waseem akhtar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
debasish tripathy;
thancks alot . It was very helpfull for me .

love to all
waseem
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm like totally lost on debasish tripathy's explanation for the third and fourth case. Can you elaborate on this?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Unicode the value of
a=\0061
b=\0062
c=\0063
d=\0064
e=\0065
f=\0066
g=\0067
h=\0068
so when the first unequal character is found the method returns the difference of their unicode values.
For "abhdef" , abdef
h-d = 68 - 64 = 4
b-d = 62 - 64 = -2
using this.charAt(k)-anotherString.charAt(k)
See the unicode values at: http://www.unicode.org/charts/PDF/U0000.pdf
[This message has been edited by Cindy Glass (edited March 19, 2001).]
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
Thanks Cindy, you have created a new angle of looking at the topic.
Zahid.
 
Ronnie Phelps
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Cindy...that was helpful for me also.
 
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic