InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Java tutorials & examples

    Java Basics

    • Hello World
    • print and println
    • System.out.format
    • static and instance variables

    Java classes and objects

    • this keyword

    Java Strings

    • Initialize string array
    • String prefix match
    • compare string
    • string to int

    Java Arrays

    • Array basics
    • 2d array example
    • array for-each loop
    • fill an array

    Java cookbooks

    • method equals vs operator ==
    • print exception as string
    • Arrays.sort custom Comparator
    • create inline Comparator
     
    • Home
    • > Tutorials
    • > Java

    Java – compare strings

    on Jun 18, 2016

    Case sensitive comparison

    Compare strings using firstString.compareTo(secondString). Positive outcome means firstString > secondString and so on.

    public class Main {
      public static void main(String[] args) {
        String firstStr = "def";
        String[] secondStrArr = {"abc", "def", "efg", "DEF"};
        System.out.println("firstStr | secondStr | firstStr.compareTo(secondStr)");
        for (String secondStr: secondStrArr) {
          System.out.println(firstStr + " | "+ secondStr + " | " + firstStr.compareTo(secondStr));
        }
      }
    }
    firstStr | secondStr | firstStr.compareTo(secondStr)
    def | abc | 3
    def | def | 0
    def | efg | -1
    def | DEF | 32
    

    Case insensitive comparison – compareToIgnoreCase

    public class MainIgnoreCase {
      public static void main(String[] args) {
        String firstStr = "def";
        String[] secondStrArr = {"abc", "def", "efg", "DEF"};
        System.out.println("firstStr | secondStr | firstStr.compareToIgnoreCase(secondStr)");
        for (String secondStr: secondStrArr) {
          System.out.println(firstStr + " | "+ secondStr + " | " + firstStr.compareToIgnoreCase(secondStr));
        }
      }
    }
    firstStr | secondStr | firstStr.compareToIgnoreCase(secondStr)
    def | abc | 3
    def | def | 0
    def | efg | -1
    def | DEF | 0
    

    Suggested posts:

    1. Java – equals vs ==
    2. Java – two dimensional array example
    3. Java System.out.print and println
    4. CSS padding inside a box
    5. Java – fill an array using java.util.Arrays
    6. Remove passphrase from ssh private key
    7. Java – array basics
    8. Mac – print java home on command line
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Java
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress