Sunday 13, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 2 May 2015

Java Example – String Compare

Following example we are going to see about how to compare two String using compareTo(String arg) , compareToIgnoreCase(String arg)  methods available in Java .
compareTo(String arg)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

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
compareToIgnoreCase(String arg)
Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by calling Character.toLowerCase(Character.toUpperCase(character)) on each character.
Sample Program
01.package com.javatutorialcorner.javastring;
02. 
03.public class CompareString {
04. 
05.    public static void main(String[] args) {
06.        String firstString = "Java Tutorials Corner";
07.        String secondString = "java tutorials corner";
08.        Object referenceObject = firstString;
09. 
10.        //Comapre
11.        System.out.println(firstString.compareTo(secondString));
12.        System.out.println(firstString.compareToIgnoreCase(secondString));
13.        System.out.println(firstString.compareTo(referenceObject.toString()));
14.         
15.    }
16. 
17.}

Output

The above sample code produce the following output

-32
0
0

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to search a word inside StringFollowing program shows how to search the word inside a string using indexOf(String str) available in Java. The indexOf(String str)  method returns the position of word with in the string if found, otherwise it returns –… Read More
  • How to get Character unicode point in a StringFollowing example shows how to get unicode point of a character in a String using str.codePointAt(int position) method. Sample Programpackage com.javatutorialcorner.javastring;public class StringUnicode { public static voi… Read More
  • String Concatenation in JavaFollowing example shows String concatenation in Java using + operator and StringBuffer.append() method. Sample Programpackage com.javatutorialcorner.javastring;public class StringConcatenation { public static void main(Str… Read More
  • How to Split the String in JavaFollowing Example shows how to split the string using str.split(String pattern) method in Java. Sample Programpackage com.javatutorialcorner.javastring;public class SplitString { public static void main(String[] args) { … Read More
  • How to convert a String to Lower caseFollowing example shows how to convert a string to lower case. Sample Programpackage com.javatutorialcorner.javastring;public class ToLowerCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java Example – String Compare Rating: 5 Reviewed By: eHowToNow