版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、1Chapter 13Strings 2Strings manipulation is one of the most common activities in computer programmingClass: StringClass: StringBufferClass: StringBuilderRegular expressions 3Introductionjava.lang.String Objects of the String class are immutable “XXXXX” is a object of the Class StringConstructor summ
2、ary of Class String String(byte bytes); String(char value); String(String original); String(StringBuffer buffer);4A example of class String: public class Test public static void main (String args) String s1 = hello; String s2 = world; String s3 = hello; System.out.println(s1 = s3); /true s1 = new St
3、ring (hello); s2 = new String (hello) ; System.out.println(s1 = s2); /false System.out.println(s1.equals(s2); /true char c = s,u,n, ,j,a,v,a; String s4 = new String(c); String s5 = new String(c,4,4); System.out.println(s4); /sun java System.out.println(s5); /java 5A example of class String:6A exampl
4、e of class String:7Methods summary of class String public char charAt(int index)public int length()public int indexOf(String str)public int indexOf(String str,int fromIndex)public boolean equalsIgnoreCase(String another)public String replace(char oldChar,char newChar)8A example of class String: publ
5、ic class Test public static void main (String args) String s1 = sun java, s2 = Sun Java; System.out.println(s1.charAt(1) ;/uSystem.out.println(s2.length() ;/8System. out. println (s1. indexOf (java) ) ; / 4System. out. println (s1. indexOf (Java) ) ; / -1System.out.println(s1.equals(s2) ;/falseSyste
6、m.out.println(s1.equalsIgnoreCase(s2); /trueString s = “He is a student. He is studying java now.;String sr = s.replace(He,She) ;System.out.println(sr) ; /Output She is a student. She is studying java now.9Methods summary of class String public boolean startsWith(String prefix)public boolean endsWit
7、h(String suffix)public String toUpperCase()public String toLowerCase()public String substring(int beginlndex)public String substring(int beginlndex,int endlndex)public String trim()10A example of class String: public class Test public static void main (String args) String s = Welcome to Java World!;
8、String s1 = sun java ;System. out .println (s.startsWith(Welcome); /trueSystem. out .println (s.endsWith(World); /falseString sL = s.toLowerCase();String sU = s.toUpperCase();System.out.println(sL) ;/welcome to java world!System.out.println(sU) ;/WELCOME TO JAVA WORLD!String subS = s.substring(11);S
9、ystem.out.println(subS) ;/Java World!String sp = s1.trim();System.out.println(sp) ;/sun java 11Methods summary of class String public static String valueOf (Object obj) public static String valueOf (boolean b) public static String valueOf (char c) public static String valueOf (int i) public static S
10、tring valueOf (long l)public string split(string regex) The method splits the string to an array of string by a specified string12public class Test public static void main(String args) int j = 1234567 ; String sNumber = String.valueOf(j) ; System.out.println(j have +sNumber.length()+ bits) ; String
11、s = Mary,F,1976; String sPlit = s.split(,); for (int i=0;isPlit.length;i+) System.out.println(sPliti) ; Output:j have 7 bitsMaryF197613Introductionjava.lang.StringBuffer A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.Constructor summary of Class
12、StringBuffer StringBuffer() StringBuffer(String str) 14Methods summary of class StringBuffer public StringBuffer append (.) public StringBuffer append(String str) public StringBuffer append(StringBuffer sbuf) public StringBuffer append(char str) public StringBuffer append (char str, int offset, int
13、len) public StringBuffer append(double d) public StringBuffer append(Object obj)Appends the string representation of the any argument to this sequence. (Object.tostring)15Methods summary of class StringBuffer public StringBuffer insert ( . ) Inserts the string representation of the any argument into
14、 this sequence. public StringBuffer insert(int offset, long l) public StringBuffer insert(int offset, double d)public StringBuffer delete (int start, int end) Removes the characters in a substring of this sequence. 16Methods summary of class StringBuffer Some methods similar as class String public i
15、nt indexOf(string str) public int indexOf(string str, int fromlndex) public String substring(int start) public String substring(int start, int end) public int length()public StringBuffer reverse () Causes this character sequence to be replaced by the reverse of the sequence. 17public class Test publ
16、ic static void main(String args) String s = Mircosoft; char a = a,b,c ; StringBuffer sbl = new StringBuffer(s); sbl.append (/).append(IBM).append(/).append (Sun); System.out.println(sbl) ; StringBuffer sb2 = new StringBuffer(nu); for(int i = 0;i=9;i+) sb2.append(i) ; System.out.println(sb2) ; sb2.de
17、lete(8,sb2.length().insert(0,a); System.out.println(sb2) ; System.out.println(sb2.reverse() ; Output: Mircosoft/IBM/Sunnu0123456789abcnu012345543210uncba18Introductionjava.lang.StringBuilder A mutable sequence of characters. No guarantee of synchronization. Used by a single thread Faster than String
18、BufferConstructor summary of Class StringBuffer StringBuilder() StringBuilder(String str) 19import java.util.*;public class UsingStringBuilder public static Random rand = new Random(47); public String toString() StringBuilder result = new StringBuilder(); for(int i = 0; i 5; i+) result.append(rand.n
19、extInt(100); result.append(, ); result.delete(result.length()-2, result.length(); result.append(); return result.toString(); public static void main(String args) UsingStringBuilder usb = new UsingStringBuilder(); System.out.println(usb); Output: 58, 55, 93, 61, 6120The different between String and S
20、tringBuilder21The different between String and StringBuilder22The different between String and StringBuilder23The different between String and StringBuilder24public class AppendStringTest public static void main(String args) String text = ; long beginTime = System.currentTimeMillis(); for(int i = 0;
21、 i 10000; i+) text = text + i; long endTime = System.currentTimeMillis(); System.out.println( Run Time: + (endTime - beginTime); StringBuilder builder = new StringBuilder( ); beginTime = System.currentTimeMillis(); for(int i = 0; i 10000; i+) builder.append(String.valueOf(i); endTime = System.curren
22、tTimeMillis(); System.out.println( Run Time: + (endTime - beginTime); 25text = text + i; -StringBuilder builder = new StringBuilder(text);builder.append(String.valueOf(i);text = builder.tostring(); 26 IntroductionRegular expressions have long been integral to standard Unix utilities like sed and awk
23、, and languages like Python and Perl Allow you to specify, programmatically, complex patterns of text that can be discovered in an input string. Solve all sorts of string processing, matching and selection, editing, and verification problems in a completely general way. 27 Character Classes:? /might
24、 or might not be appeared s /A whitespace character (space, tab, newline, form feed, carriage return) S /A non-whitespace character (s)d /A numeric digit 0-9 D /A non-digit o-9 w /A word character a-zA-Z_0-9 W /A non-word character w 28 Character Classes:. /Any character abc /Any of the characters a
25、, b, or c (same as a|b|c)abc /Any character except a, b, and c (negation) a-zA-Z /Any character a through z or A through Z (range) abchij /Any of a,b,c,h,I,j (same as a|b|c|h|i|j) (union) a-z&hij /Either h, i, or j (intersection) 29 A example of Regular expressions :public class IntegerMatch pub
26、lic static void main(String args) System.out.println(-1234.matches(-?d+); System.out.println(5678.matches(-?d+); System.out.println(+911.matches(-?d+); System.out.println(+911.matches(-|+)?d+); /* Output:truetruefalsetrue */30 A example of Regular expressions :import java.util.*;public class Splitti
27、ng public static String knights = Then, when you have found the shrubbery, you must + cut down the mightiest tree in the forest. + with. a herring!; public static void split(String regex) String arrayString = knights.split(regex); for(int i=0;iarrayString.length;i+) System.out.println(arrayStringi);
28、 31 A example of Regular expressions :public static void main(String args) /split( ); / Doesnt have to contain regex chars /split(W+); / Non-word characters split(nW+); / n followed by non-word characters /Output:Thewheyou have found the shrubbery, you must cut dowthe mightiest tree ithe forest. wit
29、h. a herring!32 A example of Regular expressions :public class Replacing static String s = Splitting.knights; public static void main(String args) System.out.println(s); System.out.println(s.replaceFirst(fw+, located); System.out.println(s.replaceAll(shrubbery|tree|herring,banana); /Output:Then, whe
30、n you have found the shrubbery, you must cut down the mightiest tree in the forest. with. a herring!Then, when you have located the shrubbery, you must cut down the mightiest tree in the forest. with. a herring!Then, when you have found the banana, you must cut down the mightiest banana in the forest. with. a banana!33 Two classesjava.util.regex Pattern A compiled representation of a regular expression. Matcher
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度個人房屋租賃定金協(xié)議及租賃合同終止補償條款2篇
- 2025年廣東高州市鑒龍水務(wù)建設(shè)投資集團有限公司招聘筆試參考題庫附帶答案詳解
- 2025版企業(yè)員工股權(quán)激勵協(xié)議(限制性股權(quán)實施)3篇
- 2025年度個人反擔(dān)保財產(chǎn)保全執(zhí)行實施合同
- 2025年全球及中國雙相抽提修復(fù)系統(tǒng)行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年全球及中國商用卡車機電開關(guān)行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年度二零二五年度高端個人雇傭員工勞動合同標(biāo)準(zhǔn)化范本4篇
- 探索現(xiàn)代青年教育的創(chuàng)新之路
- 2024食品行業(yè)食品安全風(fēng)險評估與管控合同模板3篇
- 小學(xué)數(shù)學(xué)教育中的個性化學(xué)習(xí)與自主學(xué)習(xí)結(jié)合實踐
- 液化氣安全檢查及整改方案
- 《冠心病》課件(完整版)
- 2024年廣東省事業(yè)單位考試真題及答案5
- 公園保潔服務(wù)投標(biāo)方案
- 光伏電站項目合作開發(fā)合同協(xié)議書三方版
- 禪密功筑基功法
- 2024年秋季新滬教版九年級上冊化學(xué)課件 第2章 空氣與水資源第1節(jié) 空氣的組成
- SHT+3413-2019+石油化工石油氣管道阻火器選用檢驗及驗收標(biāo)準(zhǔn)
- 香港中文大學(xué)博士英文復(fù)試模板
- 工程項目采購與供應(yīng)鏈管理研究
- 【視頻號運營】視頻號運營108招
評論
0/150
提交評論