




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、1 1 1第8章 字符串2 2 2學習目標使用String類處理固定的字符串 (8.2).使用Character類處理單個字符 (8.3).使用StringBuffer類處理可變字符串 (8.4). 如何將字符串通過命令行傳遞給main方法 (8.5).用正則表達式表示字符串匹配、替換和分解的模式(8.6).使用File類(8.7)使用 PrintWriter和Scanner 類 (8.8). 3 3 3 String +String() +String(value: String) +String(value: char) +charAt(index: int): char +compare
2、To(anotherString: String): int +compareToIgnoreCase(anotherString: String): int +concat(anotherString: String): String +endsWith(suffix: String): boolean +equals(anotherString: String): boolean +equalsIgnoreCase(anotherString: String): boolean +getChars(int srcBegin, int srcEnd, char dst, int dstBeg
3、in): void +indexOf(ch: int): int +indexOf(ch: int, fromIndex: int): int +indexOf(str: String): int +indexOf(str: String, fromIndex: int): int +lastIndexOf(ch: int): int +lastIndexOf(ch: int, fromIndex: int): int +lastIndexOf(str: String): int +lastIndexOf(str: String, fromIndex: int): int +regionMat
4、ches(toffset: int, other: String, offset: int, len: int): boolean +length(): int +replace(oldChar: char, newChar: char): String +startsWith(prefix: String): boolean +subString(beginIndex: int): String +subString(beginIndex: int, endIndex: int): String +toCharArray(): char +toLowerCase(): String +toS
5、tring(): String +toUpperCase(): String +trim(): String +copyValueOf(data: char): String +valueOf(c: char): String +valueOf(data: char): String +valueOf(d: double): String +valueOf(f: float): String +valueOf(i: int): String +valueOf(l: long): String Constructs an empty string Constructs a string with
6、 the specified string literal value Constructs a string with the specified character array Returns the character at the specified index from this string Compares this string with another string Compares this string with another string ignoring case Concat this string with another string Returns true
7、 if this string ends with the specified suffix Returns true if this string is equal to anther string Checks if this string equals anther string ignoring case Copies characters from this string into the destination character array Returns the index of the first occurrence of ch Returns the index of t
8、he first occurrence of ch after fromIndex Returns the index of the first occurrence of str Returns the index of the first occurrence of str after fromIndex Returns the index of the last occurrence of ch Returns the index of the last occurrence of ch before fromIndex Returns the index of the last occ
9、urrence of str Returns the index of the last occurrence of str before fromIndex Returns true if the specified subregion of this string exactly matches the specified subregion of the string argument Returns the number of characters in this string Returns a new string with oldChar replaced by newChar
10、Returns true if this string starts with the specified prefix Returns the substring from beginIndex Returns the substring from beginIndex to endIndex Returns a char array consisting characters from this string Returns a new string with all characters converted to lowercase Returns a new string with i
11、tself Returns a new string with all characters converted to uppercase Returns a string with blank characters trimmed on both sides Returns a new string consisting of the char array data Returns a string consisting of the character c Same as copyValueOf(data: char): String Returns a string representi
12、ng the double value Returns a string representing the float value Returns a string representing the int value Returns a string representing the long value 4 4 48.2.1 構造一個字符串String newString = new String(字符串直接量); String message = new String(Welcome to Java);快捷初始化方式:String message = Welcome to Java;5
13、5 58.2.2 永久字符串String 是永久的,它的內(nèi)容不能改變. String s = Java; s = HTML;字符串的內(nèi)容能改變嗎? s: String String object for Java s 執(zhí)行語句 String s = Java; 執(zhí)行語句 s = HTML; : String String object for Java s s: String String object for HTML 內(nèi)容不可改變 對象不能引用 6 6 6規(guī)范字符串字符串是永久的,如果兩個String對象是通過快捷初始化用相同的字符串直接量構造的,則java虛擬機為了提高效率,將它們存儲在
14、同一對象中,成為規(guī)范字符串。 可以使用String對象的intern方法返回一個規(guī)范字符串,這種字符串與使用快捷初始化創(chuàng)建的字符串相同。 7 7 7示例display s1 = s is false s2 = s is true s = s3 is true String s = Welcome to Java; String s1 = new String(Welcome to Java); String s2 = ern(); String s3 = Welcome to Java; System.out.println(s1 = s is + (s1 = s); System
15、.out.println(s2 = s is + (s2 = s); System.out.println(s = s3 is + (s = s3); : String 規(guī)范字符串對象 Welcome to Java : String String對象Welcome to Java 8 8 8代碼追蹤 String s = Welcome to Java; String s1 = new String(Welcome to Java); String s2 = ern(); String s3 = Welcome to Java; : String Interned string
16、object for Welcome to Java s 9 9 9代碼追蹤 String s = Welcome to Java; String s1 = new String(Welcome to Java); String s2 = ern(); String s3 = Welcome to Java; : String Interned string object for Welcome to Java : String A string object for Welcome to Java s s1 101010代碼追蹤 String s = Welcome to Jav
17、a; String s1 = new String(Welcome to Java); String s2 = ern(); String s3 = Welcome to Java; : String Interned string object for Welcome to Java : String A string object for Welcome to Java s s1 s2 111111代碼追蹤 String s = Welcome to Java; String s1 = new String(Welcome to Java); String s2 = s1.in
18、tern(); String s3 = Welcome to Java; : String Interned string object for Welcome to Java : String A string object for Welcome to Java s s1 s2 s3 1212127.2.3 字符串的比較 =只能檢測string1和string2是否指向同一個對象。 equalsString s1 = new String(Welcome“);String s2 = welcome; if (s1.equals(s2) / s1 and s2 have the same c
19、ontents if (s1 = s2) / s1 and s2 have the same reference 1313137.2.3 字符串的比較 compareTo(Object object)String s1 = new String(Welcome“);String s2 = welcome; if (pareTo(s2) 0) / s1 is greater than s2 else if (pareTo(s2) = 0) / s1 and s2 have the same contents else / s1 is less than s21414147.2.4 字符串長度查找
20、字符串長度用 length() 方法:message = Welcome;message.length() (returns 7)151515提取單個字符不要用 message0使用 message.charAt(index)索引從 0 W e l c o m e t o J a v a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 message 下標 message.charAt(0) message.charAt(14) message.length() is 15 1616168.2.5 字符串連接String s3 = s1.concat(s2);簡便方法:S
21、tring s3 = s1 + s2;s1 + s2 + s3 + s4 + s5 same as(s1.concat(s2).concat(s3).concat(s4).concat(s5);1717178.2.6 提取子串String 是一個永久類,改變字符串的值必須賦予一個全新的字符串.String s1 = Welcome to Java;String s2 = s1.substring(0, 11) + HTML; W e l c o m e t o J a v a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 message Indices message.
22、substring(0, 11) message.substring(11) 1818188.2.7 字符串轉(zhuǎn)換一個字符串一旦創(chuàng)建它的內(nèi)容就不能再改變,但是可以用下列方法得到一個新的字符串。 Welcome.toLowerCase() 返回welcome Welcome. toUpperCase() 返回WELCOME Welcome . trim() 返回Welcome Welcome. replace(“e”, “A”)返回WAlcomA Welcome. replaceFirst(“e”, “A”)返回WAlcome1919198.2.8獲取字符串中的一個字符或子串Welcome to
23、Java.indexOf(W) returns 0.Welcome to Java.indexOf(x) returns -1.Welcome to Java.indexOf(o, 5) returns 9.Welcome to Java.indexOf(come) returns 3.Welcome to Java.indexOf(Java, 5) returns 11.Welcome to Java.indexOf(java, 5) returns -1.Welcome to Java.lastIndexOf(a) returns 14.Welcome to Java.lastIndexO
24、f(come) returns 3. 2020208.2.9字符串和數(shù)組之間的轉(zhuǎn)換使用toCharArray方法可以將字符串轉(zhuǎn)換成一個字符的數(shù)組。例如:Char chars = “Java”.toCharArray();Chars0=J, Chars1=a, Chars2=v,Chars3=a2121218.2.10 將字符值和數(shù)值轉(zhuǎn)換成字符串valueOf 方法能夠?qū)⒆址麛?shù)組和數(shù)值轉(zhuǎn)換成字符串。valueOf的參數(shù)類型可以是 char, char, double, long, int, and float. 例如:String.valueOf(5.44). 222222舉例:檢測回文串問題:
25、 輸入一個字符串,檢測該串是否為回文串.2323238.3 字符類 Character Character +Character(value: char) +charValue(): char +compareTo(anotherCharacter: Character): int +equals(anotherCharacter: Character): boolean +isDigit(ch: char): boolean +isLetter(ch: char): boolean +isLetterOrDigit(ch: char): boolean +isLowerCase(ch: ch
26、ar): boolean +isUpperCase(ch: char): boolean +toLowerCase(ch: char): char +toUpperCase(ch: char): char 242424示例Character charObject = new Character(b);charOpareTo(new Character(a) returns 1charOpareTo(new Character(b) returns 0charOpareTo(new Character(c) returns -1charOpareTo(new Character(d) retur
27、ns 2charObject.equals(new Character(b) returns truecharObject.equals(new Character(d) returns false252525舉例:統(tǒng)計字符串中的每個字母統(tǒng)計字符串中每個字母出現(xiàn)的次數(shù),忽略字母的大小寫。 262626StringBuffer +StringBuffer() +StringBuffer(capacity: int) +StringBuffer(str: String) +append(data: char): StringBuffer +append(data: char, offset: in
28、t, len: int): StringBuffer +append(v: aPrimitiveType): StringBuffer +append(str: String): StringBuffer +capacity(): int +charAt(index: int): char +delete(startIndex: int, endIndex: int): StringBuffer +deleteCharAt(int index): StringBuffer +insert(index: int, data: char, offset: int, len: int): Strin
29、gBuffer +insert(offset: int, data: char): StringBuffer +insert(offset: int, b: aPrimitiveType): StringBuffer +insert(offset: int, str: String): StringBuffer +length(): int +replace(int startIndex, int endIndex, String str): StringBuffer +reverse(): StringBuffer +setCharAt(index: int, ch: char): void
30、 +setLength(newLength: int): void +substring(startIndex: int): String +substring(startIndex: int, endIndex: int): String 8.4 字符串緩沖區(qū)類 StringBufferStringBuffer 類比 String類更靈活,可以在字符串緩沖區(qū)中添加、插入或追加新的內(nèi)容. 272727構造字符串緩沖區(qū)public StringBuffer()沒有字符,初始容量為16個字符. public StringBuffer(int length)沒有字符,初始容量由length指定. public StringBuffer(String str)初始容量由str長度加length282828修改緩沖區(qū)中的字符串StringBuffer strBuf = new StringBuffer();strBuf.append(Welcome);strBuf.append( );strBuf.append(to);strBuf.append( );strBuf.append(Java);292929舉例
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 加盟洗衣店合同范例
- 廠房買賣意向合同范本
- 合同范本挪動字體
- 合伙承包協(xié)議合同范本
- 啤酒招商合同范例
- 噴砂合同范本
- 勞動合同范本勞動合同范本
- 商標合同范本寫
- 同城超市轉(zhuǎn)讓合同范本
- 分紅不入股協(xié)議合同范例
- GB/T 20057-2012滾動軸承圓柱滾子軸承平擋圈和套圈無擋邊端倒角尺寸
- GB/T 19808-2005塑料管材和管件公稱外徑大于或等于90mm的聚乙烯電熔組件的拉伸剝離試驗
- GB/T 10051.1-2010起重吊鉤第1部分:力學性能、起重量、應力及材料
- 2022年人民交通出版社股份有限公司招聘筆試試題及答案解析
- 班組建設工作體系課件
- 第章交通調(diào)查與數(shù)據(jù)分析課件
- 2022年江西制造職業(yè)技術學院單招語文試題及答案解析
- 穆斯林太巴熱咳慶念詞文
- 軟硬結合板的設計制作與品質(zhì)要求課件
- 中醫(yī)院情志養(yǎng)生共64張課件
- 慢性心功能不全護理查房
評論
0/150
提交評論