// common string methods
// see http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html
// Savitch: pp 82-84

public class string_methods {
    public static void main(String[] args) {
	
	String S1 = "hello";
	char[] c1 = {'h','e','l','l','o'};
	
	// Find index of character in String:
	System.out.println("1: "+ S1.indexOf('h') );
    	System.out.println("2: "+ S1.indexOf('x') );
    	System.out.println("3: "+ S1.indexOf('l') );
	if (S1.indexOf('l') == 2)
	    System.out.println("4: great (2) !");
	if (S1.indexOf('l') == 3)
	    System.out.println("4: great (3) !");
	
	// Length of String:
	System.out.println("5: "+  S1.length() );
	
	// Convert String into character array:
	char[] tmp = S1.toCharArray();
	System.out.print("6: ");
	for(int i=0;i<tmp.length;i++) System.out.print(tmp[i]);
	System.out.println();
	
	// Create String using character array:
	System.out.println("7: "+  new String(c1) );
	
	// What happens if you change a character from $c1$?
	// Nothing in $s1$ because once a String is created it cannot
	// be changed. It is as if the characters of $c1$ were supplied to
	// $s1$ as a collection of primitive types with no connection to $c1$:
	String s_chars = new String(c1);
	c1[0] = 'j';
	System.out.print("8: new char array: ");
	for(int i=0;i<c1.length;i++) System.out.print(c1[i]);
	System.out.println();
	System.out.println("9: String unchanged: "+ s_chars);
	
	// Find character at an index in a String:
	System.out.println("10: "+  S1.charAt(0) );

	// Generate uppercase String:
	System.out.println("11: "+ S1.toUpperCase() );

	// String equality:
	String s1 = new String("hi");
        String s2 = new String("hi");
	String s3 = "hi";
        String s4 = "hi";
	if (s1 == s2) System.out.println("12: test1");
	if (s1 == s3) System.out.println("12: test2");
	if (s3 == s4) System.out.println("12: test3");
    }
}

	
/* output ?
   1: 0
   2: -1
   3: 2
   4: great (2)!
   5: 5
   6: hello
   7: hello
   8: new char array: jello
   9: String unchanged: hello
   10: h
   11: HELLO
   12: test3
*/