Illuminatiiiiii
12/31/2018 - 12:34 PM

String Comparison & Search Methods

Java Library Ep. 3:

public class Main {

    public static void main(String[] args) {

        //String comparison
        String s1 = "Bob";
        String s2 = "Susan";
        String s3 = "Susan";
        String s4 = "BOB";

        System.out.println(s1 + " = " + s2 + " -> " + s1.equals(s2));
        System.out.println(s2 + " = " + s3 + " -> " + s2.equals(s3));
        System.out.println(s1 + " = " + s4 + " -> " + s1.equals(s4));
        //In this case, the case is ignored(haha, punny)
        System.out.println(s1 + " = " + s4 + " -> " + s1.equalsIgnoreCase(s4));

        //Substring Comparison
        String s5 = "Elephante";
        String s6 = "Cow";
        String s7 = "ELEPHANTE";
        System.out.println(s5 + " = " + s6 + " -> " + s5.regionMatches(2,s6,0, 3));
        //1: The index at which to start for the first string. 2: The second string 3: The index for it 4: How many chars to go for both
        System.out.println(s5 + " = " + s7 + " -> " + s5.regionMatches(true, 4,s7,4, 5));
        //In this case, the new parameter makes it ignore case

        System.out.println("Foobar".startsWith("Foo")); //Checks to see if a string starts with another string
        System.out.println("Foobar".startsWith("oo", 1)); //2: Index to start at
        System.out.println("Foobar".endsWith("bar")); //Checks the ending

        //Difference between equals and ==
        String b1 = "Hello";
        String b2 = new String(b1);
        System.out.println(b1 + " = " + b2 + " -> " + b1.equals(b2)); //Checks to see if the CHARACTERS are the same
        System.out.println(b1 + " = " + b2 + " -> " + (b1 == b2)); //Checks to see if the OBJECTS are the same

        //Find which order the word would appear in a dictionary - Lexicographical order
        System.out.println(s1.compareTo(s2));
//        returns < 0 then the String calling the method is lexicographically first
//        returns == 0 then the two strings are lexicographically equivalent
//        returns > 0 then the parameter passed to the compareTo method is lexicographically first.

        //Cool program
        String[] arr = {
                "If", "i", "like", "booty", "then", "a", "booty", "will", "like", "me"
        };
        for (int j = 0;j < arr.length;j++){
            for (int i = j + 1;i < arr.length; i++){
                if (arr[i].compareTo(arr[j]) < 0){
                    String t = arr[j];
                    arr[j] = arr[i];
                    arr[i] = t;
                }
            }
            System.out.println(arr[j]);
        }
        //To have it ignore capital letters, use compareToIgnoreCase()

        //Searching Strings

        // ---List---
        //equals() - Checks to see if the characters of the two strings match
        //equalsIgnoreCase() - Same thing but ignores the case of the letters
        //regionMatches() - compares a specific region of two strings
        //startsWith() - Determines whether a given String begins with a specified string
        //endsWith() - checks to see if the endings match
        //compareTo() - Compares two strings based on their dictionary order
        //indexOf() - Searches for the first occurrence of a character or substring
        //lastIndexOf() - Searches for the last occurrence of a character or substring

    }
}