14
JAVA 2013.01.04

2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

Embed Size (px)

Citation preview

Page 1: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

JAVA2013.01.04

Page 2: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

OUTLINE The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings Converting, replaceing, and Splitting Strings Finding a Character or a Substring in a String

Page 3: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

THE STRING CLASSA string is a sequence of characters.

The String class has 11 constructors and more than 40 methods.

Declaration:

1. String S = new String(“Hello Java”);

2. String S = “Hello Java”;

3. Char[] C = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ’ ’, ‘J’, ‘a’, ‘v’, ‘a’ };

String S = new String(C);

Page 4: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

IMMUTABLE STRINGS AND INTERNED STRINGSA string object is immutable; its content cannot be

changed.

String object for “Java”

:StringS

String object for “Java”

:StringS

String object for “html”

:String

S = “html”;String S = “Java”;

X

Page 5: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

S1.equals(S2): boolean

S1.equalsIgnoreCase(S2): boolean

S1.compareTo(S2): int

S1.compareToIgnoreCase(S2): int...

Java.lang.String

推薦網站 : http://www.tutorialspoint.com/java/java_strings.htm

STRING COMPARISONS

( 課本 p.327)

Page 6: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

STRING LENGTH, CHARACTERS, AND COMBINING STRINGS

H e l l o J a v a

0 1 2 3 4 5 6 7 8 9

S.charAt(0)

Java.lang.String

S.length(): intS.charAt(index: int): charS.concat(S2): String

S.length() = 10

S.charAt(9)

A String object is presented using an array internally.( 課本 p.365)

Page 7: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

OBTAINING SUBSTRINGS

Java.lang.String

S.substring(beginIndex: int): StringS.substring(beginIndex: int , endIndex: int): String

H e l l o J a v a

0 1 2 3 4 5 6 7 8 9

S.substring(0,5)

( 課本 p.366)

Page 8: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

程式練習 :

使用 substring method, 印出 S1 中的第 0~4 個 characters

Page 9: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

CONVERTING, REPLACEING, AND SPLITTING STRINGS

Java.lang.String

S.toLowerCase(): String

S.toUpperCase(): String

S.trim(): String

S.replace(oldchar, newchar): String

S.replaceFirst(oldString, newString): String

S.replaceAll(oldString, newString): String

S.split (delimiter: String): String[ ]( 課本 p.367)

Page 10: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

程式練習 :

1. 將 S1 轉成 lowercase 印出2. 將 S2 轉成 uppercase 印出3. 將 S1 中的” JAVA” 子字串 換成” C++” 印出4. 嘗試字串分割

String[] names = S1.split(" ");

for(String name:names)

{

System.out.println(name);

}

Page 11: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

FINDING A CHARACTER OR A SUBSTRING IN A STRING

Java.lang.String

“JAVA”.indexOf(‘J’) return 0.

“JAVA”.indexOf(‘A’) return 1.

“JAVA”.indexOf(‘A’,2) return 3.

“JAVA”.indexOf(”VA”) return 2.

“JAVA”.indexOf(”ABC”,2) return -1.

“JAVA”.lastIndexOf(‘A’) return 3.... ( 課本 p.368)

Page 12: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

程式練習 :

從 S1 找出子字串” JAVA” 印出回傳值

Page 13: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

PRACTICE(Anagrams) Write a method that checks whether two words are anagrams. Two words are anagrams if they contain the same letters in any order. For example,"silent" and "listen" are anagrams. The header of the method is as follows:

public static boolean isAnagram(String s1, String s2) Write a test program that prompts the user to enter two strings and, if they are anagrams, displays "anagram", otherwise displays "not anagram".

Page 14: 2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings

Ppt下載: http://oss.csie.fju.edu.tw/~jastine01/

ppt.html