JAVA String類經(jīng)常用方法
作者:翼風(fēng)Ephonre tags:Java String str java String ing
1.charAt 方法,返回指定index的字符。 String string ="123456789"; char a =string.charAt(2); System.out.print(a); 2.codePointAt 方法,返回指定索引處的字符(Unicode 代碼點(diǎn))。 string ="abc"; int codePoint=string.codePointAt(2); System.out.print(codePoint); 3.codePointCount 字符的長(zhǎng)度 引用: 在JDK5.0中,Java引入了對(duì)Unicode4.0中所謂"增補(bǔ)碼"的支持,這種字符的長(zhǎng)度大于2B,因此用char類型無(wú)法表示(Java中char類型占2B). Java的設(shè)計(jì)者于是利用兩個(gè)char變量空間來(lái)表示這樣的一個(gè)字符 于是,假如字符串中有這樣的字符,則length方法無(wú)法精確的表示字符長(zhǎng)度 (length方法返回的是String中char的個(gè)數(shù)) ,于是有了codePointCount方法,利用這個(gè)方法可以精確統(tǒng)計(jì)出String中字符的數(shù)量,考慮到了String中可能含有的"增補(bǔ)碼" 4.compareTo 字符串的比較 假如2個(gè)字符串相同,返回值為0; string ="abcdefg"; int codePoint=string.compareTo("ea"); 將返回e在ea中的索引0減去e在“abcdefg”中的索引4,所以codePoint=-4 引用: 按字典順序比較兩個(gè)字符串。該比較基于字符串中各個(gè)字符的 Unicode 值。按字典順序?qū)⒋? String 對(duì)象表示的字符序列與參數(shù)字符串所表示的字符序列進(jìn)行比較。假如按字典順序此 String 對(duì)象位于參數(shù)字符串之前,則比較結(jié)果為一個(gè)負(fù)整數(shù)。假如按字典順序此 String 對(duì)象位于參數(shù)字符串之后,則比較結(jié)果為一個(gè)正整數(shù)。假如這兩個(gè)字符串相等,則結(jié)果為 0;compareTo 只在方法 equals(Object) 返回 true 時(shí)才返回 0。 這是字典排序的定義。假如這兩個(gè)字符串不同,那么它們要么在某個(gè)索引處的字符不同(該索引對(duì)二者均為有效索引),要么長(zhǎng)度不同,或者同時(shí)具備這兩種情況。假如它們?cè)谝粋€(gè)或多個(gè)索引位置上的字符不同,假設(shè) k 是這類索引的最小值;則在位置 k 上具有較小值的那個(gè)字符串(使用 < 運(yùn)算符確定),其字典順序在其他字符串之前。在這種情況下,compareTo 返回這兩個(gè)字符串在位置 k 處兩個(gè)char 值的差 5.compareToIgnoreCase 忽略大小寫(xiě) 6.indexOf(int ch)ch:unicode code point,假如字符串中沒(méi)有ch,則返回-1String ss = "abcde"; System.out.println(ss.indexOf(2)); System.out.println(ss.indexOf(98)); 結(jié)果:-1 1 因?yàn)?對(duì)應(yīng)的unicode在字符串ss中不存在,所以返回值-1,98對(duì)應(yīng)的unicode 是b,所以返回值是index=1 7.indexOf(String str) 返回參數(shù)在字符串中第一次出現(xiàn)的位置索引 string ="abcaabc"; int codePoint=string.indexOf("bc"); 結(jié)果: index =1 8.indexOf(int ch, int fromIndex) ch (Unicode) String string ="abcaabc"; int codePoint=string.indexOf(99, 5); System.out.println(codePoint); System.out.println(string.indexOf(99, 5)); 結(jié)果: 6 2 因?yàn)閏對(duì)應(yīng)的Unicode為99 indexOf(String str,int fromIndex) 返回從索引fromIndex開(kāi)始,str第一次出現(xiàn)的位置 String string ="abcaabc"; System.out.println(string.indexOf("bc", 0)); System.out.println(string.indexOf("bc", 5)); 結(jié)果: 1 5 lastIndexOf(int ch)返回 unicode 在字符串中最后出現(xiàn)時(shí)的位置索引 String string ="abcaabc"; System.out.println( 結(jié)果: 6 lastIndexOf(String str)返回 str 在字符串中最后出現(xiàn)時(shí)的位置索引 String string ="abcaabc"; System.out.println(string.lastIndexOf("bc")); 結(jié)果:5 lastIndexOf(int ch, int fromIndex)返回從0到fromIndex,ch最后出現(xiàn)的位置索引String string ="abcdafaa";System.out.println(string.lastIndexOf(97, 4)); 返回值<=fromIndex 結(jié)果:4 lastIndexOf(int ch, int fromIndex)返回從0到fromIndex,str最后出現(xiàn)的位置索引String string ="abcbcbcbcbc";System.out.println(返回值 k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)結(jié)果:4 9.offsetByCodePoints(int index,int codePointOffset) codePointOffset 偏移量 string ="abcaabcdef漢子"; int codePoint=string.offsetByCodePoints(8, 2); 結(jié)果: codePoint=10 引用解釋: 一個(gè)完整的Unicode字符叫代碼點(diǎn)/CodePoint,而一個(gè)Java char 叫代碼單元code unit; string對(duì)象以UTF-16保存Unicode字符,需要用2個(gè)字符表示一個(gè)超大字符集漢字,這種表示方式為 Sruuogate,第一個(gè)字符叫Surrogate High,第二個(gè)就是Surrogate Low 判定一個(gè)char是否是Surrogate區(qū)的字符,用Character的isHighSurrogate()/isLowSurrogate()方法。 從兩個(gè)Surrogate High/Low字符,返回一個(gè)完整的Unicode CodePoint用Character.toCodePoint()/codePointAt() 一個(gè)Code Point,可能需要一個(gè)也可能需要兩個(gè)char表示,因此不能直接使用CharSequence.length() 方法返回一個(gè)字符串到底有多少個(gè)漢字,而需要用String.codePointCount()/Character.codePointCount() 要定位字符串中的第N個(gè)字符,不能直接將n作為偏移量,而需要從字符串頭部依次遍歷得到,需要 String.offsetByCodePoints() 從字符串的當(dāng)前字符,找到上一個(gè)字符,不能直接用offset實(shí)現(xiàn),而需要 String.codePointBefore(),或String.offsetByCodePoints() 從當(dāng)前字符,找下一個(gè)字符,需要判定當(dāng)前CodePoint的長(zhǎng)度,再計(jì)算得到 String.offsetByCodePoints()。 個(gè)人測(cè)試:假如沒(méi)有拋出異常,返回值總是index + codePointOffset 10.concat(String str)將參數(shù)連接到字符串的末尾 concatenate 如鎖鏈般連續(xù),使連鎖,連結(jié)string ="abc";System.out.print(string.concat("123")); 結(jié)果:abc123 假如str的length是0,那么這個(gè)String就會(huì)被返回。 11.intern 字符串扣留 返回字符串對(duì)象的規(guī)范化表示形式。 java API解釋:
String string1 = "Too many"; |
|