Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "2013.01.04. The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters, and Combining Strings Obtaining Substrings."— Presentation transcript:

1 2013.01.04

2 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

3 A 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);

4 A 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

5 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 ( p.327)

6 HelloJava 0 1 2 3 4 5 6 7 8 9 S.charAt(0) Java.lang.String S.length(): int S.charAt(index: int): char S.concat(S2): String S.length() = 10 S.charAt(9) A String object is presented using an array internally. ( p.365)

7 Java.lang.String S.substring(beginIndex: int): String S.substring(beginIndex: int, endIndex: int): String HelloJava 0 1 2 3 4 5 6 7 8 9 S.substring(0,5) ( p.366)

8 : substring method, S1 0~4 characters

9 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)

10 : 1. S1 lowercase 2. S2 uppercase 3. S1 JAVA C++ 4. String[] names = S1.split(" "); for(String name:names) { System.out.println(name); }

11 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)

12 : S1 JAVA

13 (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".

14 Ppt http://oss.csie.fju.edu.tw/~jastine01/ppt.html


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

Similar presentations


Ads by Google