9-1  StringReplace.java
// Substring replacement.
class StringReplace {
	public static void main(String args[]) {
		String org = "It is a cat, is it?";
		String search = "is";
		String sub = "was";
		String result = "";
		int i;
		do { // replace all matching substrings
			System.out.println(org);
			i = org.indexOf(search);
			if (i != -1) {
				result = org.substring(0, i);
				result = result + sub;
			result = result + org.substring(i + search.length());
				org = result;
			}
		} while (i != -1);
	}
}

9-2  Secret.java
class Secret {
	public static void main(String args[]) {
		String s = "ʮͷ";
		char a[] = s.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 's');
		}
		String secret = new String(a);
		System.out.println(":" + secret);
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 's');
		}
		String code = new String(a);
		System.out.println("ԭ:" + code);
	}
}


9-3  UseTrim.java
import java.io.*;
class UseTrim {
	public static void main(String args[]) throws IOException {
		// create a BufferedReader using System.in
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str;
		System.out.println("Enter 'stop' to quit.");
		System.out.println("Enter State: ");
		do {
			str = br.readLine();
			str = str.trim(); // remove whitespace
			if (str.equals("Illinois"))
			System.out.println("Capital is Springfield.");
			else if (str.equals("Missouri"))
			System.out.println("Capital is Jefferson City.");
			else
			System.out.println("Enter State,again");
		} while (!str.equals("stop"));
	}
}


9-4  Cvaculator.java
import javax.swing.JOptionPane;
public class Caculator {

	public static void main(String[] args) {
String operation = JOptionPane.showInputDialog(
"ҪִеĲ:+ - * /");
		String num1 = JOptionPane.showInputDialog("һ");
		String num2 = JOptionPane.showInputDialog("ڶ");
		try {
JOptionPane.showMessageDialog(null, num1 + operation + num2 + "="
				+ processOperation(operation, num1, num2));
		} catch (Throwable e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}

	public static double processOperation(String operation, String num1,
			String num2) throws Throwable {
		double arg1 = Double.parseDouble(num1);
		double arg2 = Double.parseDouble(num2);
		double result;
		if ("+".equals(operation)) {
			result = add(arg1, arg2);
		} else if ("-".equals(operation)) {
			result = subtract(arg1, arg2);
		} else if ("*".equals(operation)) {
			result = multiply(arg1, arg2);
		} else if ("/".equals(operation)) {
			if (arg2 == 0)
				throw new Throwable("Ϊ");
			result = divide(arg1, arg2);
		} else {
			throw new Throwable("δ֪+-*/");
		}
		return result;
	}

	private static double divide(double arg1, double arg2) {
		return arg1 / arg2;
	}

	private static double multiply(double arg1, double arg2) {
		return arg1 * arg2;
	}

	private static double subtract(double arg1, double arg2) {
		return arg1 - arg2;
	}

	private static double add(double arg1, double arg2) {
		return arg1 + arg2;
	}

}


9-5  StringBufferText.java
class StringBufferText {
	public static void main(String args[]) {
		StringBuffer sb = new StringBuffer("Java");
		System.out.println("buffer = " + sb);
		System.out.println("length = " + sb.length());
		System.out.println("capacity = " + sb.capacity());
	}
}


9-6  UseAppend.java
class UseAppend {
	public static void main(String args[]) {
		String s;
		int x = 10;
		StringBuffer sb = new StringBuffer(40);
	s = sb.append("x = ").append(x).append(",").toString();
		System.out.println(s);
	}
}

9-7  Decode.java
import javax.swing.JOptionPane;
public class Decode {

	public static void main(String[] args) {
		String secret = JOptionPane.showInputDialog("");
		JOptionPane.showInputDialog("ܺĽ", decode(secret));
	}

	public static String decode(String secret) {
		char a[] = secret.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String text = new String(a);
		return text;
	}
}

public class Encode {

	public static void main(String[] args) {
		String text = JOptionPane.showInputDialog("ԭ");
		JOptionPane.showInputDialog("ܺĽ", encode(text));
	}

	public static String encode(String text) {
		char a[] = text.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String secret = new String(a);
		return secret;
	}

}

9-8  Test.java  
import java.util.*;
import java.awt.*;
import java.awt.event.*;

class Example extends Frame implements TextListener {
	TextArea text1, text2;
	public Example() {
		setLayout(new BorderLayout());// ʽ
		text1 = new TextArea(6, 15);
		text2 = new TextArea(6, 15);
		add(BorderLayout.NORTH, text1);
		add(BorderLayout.CENTER, text2);
		text2.setEditable(false);// ıı༭
		text1.addTextListener(this);// ¼
		setVisible(true);// ڿɼ
		this.setBounds(145, 67, 178, 585);// ڴС
	}
	public void textValueChanged(TextEvent e) {
		if (e.getSource() == text1) {
			String s = text1.getText();
		StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
			int n = fenxi.countTokens();
			String a[] = new String[n];
			for (int i = 0; i <= n - 1; i++) {
				String temp = fenxi.nextToken();
				a[i] = temp;
			}
		for (int i = 0; i <= n - 1; i++) // ֵС
			{
				for (int j = i + 1; j <= n - 1; j++) {
					if (a[j].compareTo(a[i]) < 0) {
						String t = a[j];
						a[j] = a[i];
						a[i] = t;
					}
				}
			}
			text2.setText(null); // ˢʾ
			for (int i = 0; i < n; i++) {
				text2.append(a[i] + "\n");
			}
		}
	}
}

public class Test {
	public static void main(String args[]) {
		new Example();
	}
}

9-9 ַתtoString( )
class Box {
	double width;
	double height;
	double depth;

	Box(double w, double h, double d) {
		width = w;
		height = h;
		depth = d;
	}

	public String toString() {
		return "Dimensions are " + width + " by " + depth + " by " + height+ ".";
	}
}

class toStringDemo {
	public static void main(String args[]) {
		Box b = new Box(10, 12, 14);
		String s = "Box b: " + b; // concatenate Box object
		System.out.println(b); // convert Box to string
		System.out.println(s);
	}
}
9-10 ȡַgetChars( ) 
class getCharsDemo {
	public static void main(String args[]) {
		String s = "This is a demo of the getChars method.";
		int start = 10;
		int end = 14;
		char buf[] = new char[end - start];

		s.getChars(start, end, buf, 0);
		System.out.println(buf);
	}
}
9-11ַȽequals( ) equalsIgnoreCase( )
class equalsDemo {
	public static void main(String args[]) {
		String s1 = "Hello";
		String s2 = "Hello";
		String s3 = "Good-bye";
		String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> "
				+ s1.equalsIgnoreCase(s4));
	}
}
9-12 startsWith( )endsWith( )
class StartsWithDemo {
	public static void main(String args[]) {
		String s3 = "Good-bye";
		System.out.println(s3.startsWith("oo"));
		System.out.println(s3.endsWith("y"));
	}
}
9-13 equals( )==ıȽ
class EqualsNotEqualTo {
	public static void main(String args[]) {
		String s1 = "Hello";
		String s2 = new String(s1);

System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
	}
}
9-14ַȽcompareTo( )
һַӳ򡣳ðݷʹcompareTo( )ȷ˳
class SortString {
static String arr[] = { "Now", "is", "the", "time", "for", "all", "good","men", "to", "come", "to", "the", "aid", "of", "their", "country" };

	public static void main(String args[]) {
		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]);
		}
	}
}
9-15ַȽindexOf( )
˵òַͬStringڲ
class indexOfDemo {
	public static void main(String args[]) {
		String s = "Now is the time for all good men "
				+ "to come to the aid of their country.";
		System.out.println(s);
		System.out.println("indexOf(t) = " + s.indexOf('t'));
		System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t'));
		System.out.println("indexOf(the) = " + s.indexOf("the"));
		System.out.println("lastIndexOf(the) = " + s.lastIndexOf("the"));
		System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));
		System.out.println("lastIndexOf(t, 60) = " + s.lastIndexOf('t', 60));
		System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
		System.out
				.println("lastIndexOf(the, 60) = " + s.lastIndexOf("the", 60));
	}
}


9-16 TextFilter.java
public class TextFilter {

	public static void main(String[] args) {
		String text = "It's absolutely crazy!He must be abashed. ";
		System.out.println("ԭ:" + text);
System.out.println("μ:" + checkText(text, "ab", "**"));

		String[] keyWords = { "ab", "c", "he" };
		System.out.println("ԭ:" + text);
		System.out.println(checkText(text, keyWords, "**"));
	}

	/**
	 * ÷һ
	 * 
	 * @param text
	 *            Ҫı
	 * @param keyWord
	 *            Ҫε
	 * @param replacement
	 *            滻
	 * @return κĽ
	 */
	public static String checkText(String text, String keyWord,
			String replacement) {
		text = text.replaceAll(keyWord, replacement);
		return text;
	}

	/**
	 * ÷ζ
	 * 
	 * @param text
	 *            Ҫı
	 * @param keyWords
	 *            Ҫε
	 * @param replacement
	 *            滻
	 * @return κĽ
	 */

	public static String checkText(String text, String[] keyWords,
			String replacement) {
		for (int i = 0; i < keyWords.length; i++) {
			text = text.replaceAll(keyWords[i], replacement);
		}
		return text;
	}
}












