Lecture 9 Part 2: String

Reference

String API

Reading

Characters and Strings(and its links) (only read the part about Strig)

Length of String, characters of strings and substrings

Example
What is the output of the following program:
public class StringTest1 {
    public static void main(String[] args) {
        String s = "This is a test.";
	System.out.println(s.length());
	System.out.println(s.charAt(3));
	System.out.println(s.substring(3));
	System.out.println(s.substring(3,7));
	char[] charray = s.toCharArray();
	System.out.println(charray[5]);
    }
}
Answer:
15
s
s is a test.
s is
i

Write a program to count the number of 'a' appears in the following paragraph :
The changing of the guard began in earnest Thursday with 
Arnold Schwarzenegger introducing  a team led by a veteran 
political insider that will shepherd the actor's transition to governor 
as soon as this week's recall election is certified.
 
public class StringTest2 {
    public static void main(String[] args) {
       // break the line in few strings
           String s = "The changing of the guard began in earnest Thursday with " + 
	                   "Arnold Schwarzenegger introducing a team led by a veteran " +
			   "political insider that will shepherd the actor's transition to governor " +
			   "as soon as this week's recall election is certified.";
	int count = 0;
	for(int i = 0; i < s.length(); i++) {
	    if(s.charAt(i) == 'a') {
		count++;
	    }
	}
	System.out.println("There are " + count + "a.");
    }
}
Output
There are 17a.

Searching

What is the output of the following program?
public class StringTest3 {
    public static void main(String[] args) {
	String s = "C++ is a good computer language. Java is the best computer language!"; 
	
	System.out.println(s.indexOf('a'));
	System.out.println(s.indexOf('a', 20));
	System.out.println(s.lastIndexOf('u'));
	System.out.println(s.lastIndexOf('o', 20));
	System.out.println(s.indexOf("computer"));
	System.out.println(s.lastIndexOf("computer"));
	System.out.println(s.lastIndexOf("abcdefg"));
    }
}
Answer
7
24
63
15
14
50
-1
For the following list of names, write a program to print out the last names:
String[] names = {"George Washington", "Abraham Lincoln", "Bill Clinton", "Ronald Reagan","Thomas Jefferson", "George Bush"};
Answer
public class LastName {
    public static void main(String[] args) {
        String[] names = {"George Washington", "Abraham Lincoln", "Bill Clinton",
                               "Ronald Reagan","Thomas Jefferson", "George Bush"};
        for(int i =0; i < names.length; i++) {
	    System.out.println(names[i].substring(names[i].lastIndexOf(" ")+1));
	}
    }
}
Output
Washington
Lincoln
Clinton
Reagan
Jefferson
Bush

Test

Creating new String

What is the output of the following program?
public class StringTest4 {
    public static void main(String[] args) {
	String s = "PIC10A is cool!";
	System.out.println(s);
	System.out.println(s.toLowerCase());
	System.out.println(s.toUpperCase());
	System.out.println(s.replace('o','a'));
	System.out.println(s);
    }
}
Output
PIC10A is cool!
pic10a is cool!
PIC10A IS COOL!
PIC10A is caal!
PIC10A is cool!
Remark You can't change the content of a String. You can only assign a new reference to it.