/******************************************************************************
 * Example of String methods
 *     equal()      - checking for string equality
 *     indexOf()    - finding parts of a string
 *     substring()  - extracting substrings from a string
 *     replace()    - replacing certain characters
 *     toCharArray()    - converting string to a character array
 *     String.valueOf() - converting character array to a string
 *     split()      - split a string into an array of strings
 *     compareTo()  - compare 2 strings
******************************************************************************/

public class string2
{
    public static void main (String[] args) 
    {
    //---- checking for equality ---------------------------------------
    
        String name  = "Sam Sultan";                    

        String first = "Sam";
        String last  = "Sultan";
        String full  =  first + " " + last;

        if (name == full) 
            System.out.println(name + "," + full + " are Equal using ==");
        else
            System.out.println(name + "," + full + " are NOT Equal using ==");


        if (name.equals(full)) 
            System.out.println(name + "," + full + " are Equal using equal()");
        else
            System.out.println(name + "," + full + " are NOT Equal using equal()");

        System.out.println();


    //---- Finding parts of strings ---------------------------------------

        String text = "the quick brown fox jumped over the lazy dog";                   
        System.out.println(text + '\n');

        int findIt;

        findIt = text.indexOf("over");              
        if (findIt >= 0)                    
            System.out.println("Found 'over' at: " + findIt);
        else
            System.out.println("'over' Not Found " + findIt);
        
        findIt = text.indexOf("cat");
        if (findIt >= 0)
            System.out.println("Found 'cat' at: " + findIt);
        else
            System.out.println("'cat' Not Found " + findIt);

        findIt = text.indexOf("the");
        System.out.println("Found 'the' at: " + findIt);

        findIt = text.indexOf("the", 1);                    //with an offset
        System.out.println("Found 'the' at: " + findIt);

        System.out.println();


    //---- Extracting Substrings ---------------------------------------

        String sub1 = text.substring(32);
        System.out.println("from 32 until the end:  " + sub1); 

        String sub2 = text.substring(20,31);
        System.out.println("from position 20 to 30: " + sub2); 

        String sub3 = text.substring(0,20);
        System.out.println("from position 0 to 19 : " + sub3);

        String text2 = sub1 + " " + sub2 + " " + sub3;
        System.out.println('\n' + "Reassembling the String from the extracted parts:");  
        System.out.println(text2);  


    //---- Replacing characters and strings -----------------------------------

        String text3; 
        text3 = text.replace(' ', '-');
        text3 = text3.replace('o','O');
        System.out.println('\n' + text3);  

        String text4; 
        text4 = text.replaceAll("the", "THAT");
        System.out.println('\n' + text4);  


    //---- Converting String to/from an array of characters --------------------

        char[ ] letters; 

        letters = text.toCharArray();               //convert String to char array
     
        for (int i=0; i<letters.length; i++) 
            System.out.println(letters[i]);

        String text5 = String.valueOf(letters);     //convert char array to String

        System.out.println('\n' + "Reassembling the String from the character array:");  
        System.out.println(text5);      

        System.out.println();


    //---- Splitting a long String into an array of Strings  ------------------

//      String[ ] words = text.split(" ");          //split on a space               
        String[ ] words = text.split("\\s+");       //split on 1 or more spaces - Regex 

        for (int i=0; i<words.length; i++) 
            System.out.println(words[i]);

        System.out.println();


    //---- Comparing and Switching Strings  -------------------------------------

        String string1  =  "Sam";
        String string2  =  "John";
        String temp;

        System.out.println("Before: " + string1 + ", " + string2);  //Before: Sam, John
    
        int x = string1.compareTo(string2);                         //in this case, returns 9 
                                                                    //S is 9 char more than J
        System.out.println("the 2 words are: " + x + " char apart"); 

        if ( string1.compareTo(string2)  >  0 )                 
        {                               
            temp    = string1;                                      //switch the 2 strings                  
            string1 = string2;                  
            string2 = temp;
        }

        System.out.println("After: " + string1 + ", " + string2);   //After: John, Sam

        System.out.println();

    }
}