Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-17-2007, 08:40 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
String comparison
Hi friends,
i have a doubt about string comparison.
when we use the method like..
Code:
result=st1.compareTo(st2);
then...
result>0 if st1>st2
result=0 if st1=st2
result<0 if st1<st2

how can we compare strings like numbers ?
how this method works ?
what is the use of string comparison ?
please tell me.....
thankq ...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-18-2007, 01:16 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
You can use the compareTo method for class instances/objects that implement the Comparable interface, egs, String and Integer.
Code:
String s1 = "hello world"; String s2 = "goodbye"; int retVal = s1.compareTo(s2); boolean equal = s1.equals(s2); String s3 = "95"; String s4 = "101"; // Which number is larger? Integer i3 = Integer.valueOf(s3); int n4 = Integer.parseInt(s4); Integer i4 = Integer.valueOf(n4); int compare = i3.compareTo(i4); System.out.println("compare = " + compare); System.out.printf("i3 is %s i4%n", (compare < 1) ? "less than" : (compare > 1) ? "greater than" : "equal to"); int n3 = i3.intValue(); System.out.printf("n3 < n4 = %b%n", n3 < n4);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
String comparison abhiN New To Java 2 04-09-2008 05:47 AM
Comparison of Strings Cero.Uno New To Java 3 02-11-2008 03:46 AM
Date comparison Rageagainst20 New To Java 0 12-19-2007 07:34 PM
Parsing Dates for Comparison Rageagainst20 New To Java 1 12-19-2007 06:50 AM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 05:59 PM


All times are GMT +3. The time now is 11:07 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org