Compares two strings provided and returns an expected integer value based on the comparison - Java java.lang

Java examples for java.lang:String Compare

Description

Compares two strings provided and returns an expected integer value based on the comparison

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str1 = "java2s.com";
        String str2 = "java2s.com";
        System.out.println(compareStrings(str1, str2));
    }//from w w  w .j a v  a2 s .  c om

    /**
     * Compares two strings provided and returns an expected integer value based on the comparison
     * @param str1
     * @param str2
     * @return int
     */
    public static int compareStrings(String str1, String str2) {
        if (str1 == null)
            return -1;
        if (str2 == null)
            return -1;

        if (str1.length() != str2.length())
            return -1;

        return str1.compareTo(str2);
    }
}

Related Tutorials