JAVA String類(lèi)經(jīng)常用方法作者:翼風(fēng)Ephonre tags:
Java String str java String ing1.charAt 方法,返回指定index的字符。
Stringstring ="123456789";
char a =string.charAt(2);
System.out.print(a);
a=32.codePointAt 方法,返回指定索引處的字符(Unicode代碼點(diǎn))。string ="abc";
int codePoint=string.codePointAt(2);
System.out.print(codePoint);
結果:993.codePointCount 字符的長(cháng)度引用:
在JDK5.0中,Java引入了對Unicode4.0中所謂"增補碼"的支持,這種字符的長(cháng)度大于2B,因此用char類(lèi)型無(wú)法表示(Java中char類(lèi)型占2B).
Java的設計者于是利用兩個(gè)char變量空間來(lái)表示這樣的一個(gè)字符
于是,假如字符串中有這樣的字符,則length方法無(wú)法精確的表示字符長(cháng)度 (length方法返回的是String中char的個(gè)數),于是有了codePointCount方法,利用這個(gè)方法可以精確統計出String中字符的數量,考慮到了String中可能含有的"增補碼"4.compareTo 字符串的比較假如2個(gè)字符串相同,返回值為0;
string ="abcdefg";
int codePoint=string.compareTo("ea");
將返回e在ea中的索引0減去e在“abcdefg”中的索引4,所以
codePoint=-4引用:
按字典順序比較兩個(gè)字符串。該比較基于字符串中各個(gè)字符的 Unicode 值。按字典順序將此String 對象表示的字符序列與參數字符串所表示的字符序列進(jìn)行比較。假如按字典順序此 String對象位于參數字符串之前,則比較結果為一個(gè)負整數。假如按字典順序此 String對象位于參數字符串之后,則比較結果為一個(gè)正整數。假如這兩個(gè)字符串相等,則結果為 0;compareTo 只在方法equals(Object) 返回 true 時(shí)才返回 0。
這是字典排序的定義。假如這兩個(gè)字符串不同,那么它們要么在某個(gè)索引處的字符不同(該索引對二者均為有效索引),要么長(cháng)度不同,或者同時(shí)具備這兩種情況。假如它們在一個(gè)或多個(gè)索引位置上的字符不同,假設k 是這類(lèi)索引的最小值;則在位置 k 上具有較小值的那個(gè)字符串(使用 <運算符確定),其字典順序在其他字符串之前。在這種情況下,compareTo 返回這兩個(gè)字符串在位置 k 處兩個(gè)char值的差5.compareToIgnoreCase 忽略大小寫(xiě)6.indexOf(int ch)
ch:unicode code point,假如字符串中沒(méi)有ch,則返回-1
String ss = "abcde";
System.out.println(ss.indexOf(2));
System.out.println(ss.indexOf(98));
結果:-11
因為2對應的unicode在字符串ss中不存在,所以返回值-1,98對應的unicode是b,所以返回值是index=1
7.indexOf(String str) 返回參數在字符串中第一次出現的位置索引string ="abcaabc";
int codePoint=string.indexOf("bc");
結果:index =18.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));
結果:6
2因為c對應的
Unicode為99indexOf(String str,int fromIndex)返回從索引fromIndex開(kāi)始,str第一次出現的位置String string ="abcaabc";
System.out.println(string.indexOf("bc", 0));
System.out.println(string.indexOf("bc", 5));
結果:1
5lastIndexOf(int ch)返回 unicode在字符串中最后出現時(shí)的位置索引String string ="abcaabc";
System.out.println(
string.lastIndexOf(99));//99 is unicode of c
結果:
6
lastIndexOf(Stringstr)返回 str 在字符串中最后出現時(shí)的位置索引
String string="abcaabc";
System.out.println(string.lastIndexOf("bc"));
結果:5
lastIndexOf(int ch, intfromIndex)返回從0到fromIndex,ch最后出現的位置索引
String string ="abcdafaa";
System.out.println(string.lastIndexOf(97, 4));
返回值<=fromIndex
結果:4
lastIndexOf(int ch, intfromIndex)返回從0到fromIndex,str最后出現的位置索引
String string ="abcbcbcbcbc";
System.out.println( string.lastIndexOf("bc", 6));
返回值
k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
結果:4
9.offsetByCodePoints(int index,intcodePointOffset)
codePointOffset 偏移量
string ="abcaabcdef漢子";
int codePoint=string.offsetByCodePoints(8, 2);
結果:
codePoint=10
引用解釋?zhuān)?br>一個(gè)完整的Unicode字符叫代碼點(diǎn)/CodePoint,而一個(gè)Java char叫代碼單元code unit;
string對象以UTF-16保存Unicode字符,需要用2個(gè)字符表示一個(gè)超大字符集漢字,這種表示方式為
Sruuogate,第一個(gè)字符叫Surrogate High,第二個(gè)就是Surrogate Low
判定一個(gè)char是否是Surrogate區的字符,用Character的isHighSurrogate()/isLowSurrogate()方法。
從兩個(gè)Surrogate High/Low字符,返回一個(gè)完整的UnicodeCodePoint用Character.toCodePoint()/codePointAt()
一個(gè)CodePoint,可能需要一個(gè)也可能需要兩個(gè)char表示,因此不能直接使用CharSequence.length()
方法返回一個(gè)字符串到底有多少個(gè)漢字,而需要用String.codePointCount()/Character.codePointCount()
要定位字符串中的第N個(gè)字符,不能直接將n作為偏移量,而需要從字符串頭部依次遍歷得到,需要
String.offsetByCodePoints()
從字符串的當前字符,找到上一個(gè)字符,不能直接用offset實(shí)現,而需要
String.codePointBefore(),或String.offsetByCodePoints()
從當前字符,找下一個(gè)字符,需要判定當前CodePoint的長(cháng)度,再計算得到
String.offsetByCodePoints()。
個(gè)人測試:假如沒(méi)有拋出異常,返回值總是index + codePointOffset
10.concat(Stringstr)將參數連接到字符串的末尾
concatenate如鎖鏈般連續,使連鎖,連結
string ="abc";
System.out.print(string.concat("123"));
結果:abc123
假如str的length是0,那么這個(gè)String就會(huì )被返回。
11.intern 字符串扣留 返回字符串對象的規范化表示形式。java API解釋:
public String intern()
一個(gè)初始時(shí)為空的字符串池,它由類(lèi) String 私有地維護。
當調用 intern 方法時(shí),假如池已經(jīng)包含一個(gè)等于此 String 對象的字符串(該對象由equals(Object) 方法確定),則返回池中的字符串。否則,將此String 對象添加到池中,并且返回此 String 對象的引用。
它遵循對于任何兩個(gè)字符串 s 和 t,當且僅當s.equals(t) 為 true時(shí),s.intern() == t.intern()才為true。
String string1 = "Too many";
String string2 = " cooks";
String string3="Too many cooks";
String string4 =string1 +string2;
System.out.println(string3==string4);
string4=string4.intern();
System.out.println(string3==string4);
結果:
false
true
12 hashCode 返回字符串的hashCode值
String string0 ="abc";
String string1 ="abc";
System.out.println(string1.hashCode());
System.out.println(string1.hashCode());
結果:
96354
96354
13 contains(CharSequences)是否包含參數
String string0 ="abcdef";
System.out.println( string0.contains("de"));
結果:true
14 contentEquals(CharSequencecs)
將此字符串與指定的 CharSequence比較。當且僅當此 String 與指定序列表示相同的 char 值序列時(shí),結果才為true。
參數 cs -要與此 String 比較的序列
返回:假如此String 與指定序列表示相同的 char 值序列,則返回 true;否則返回false。
String string0 ="abcdef";
System.out.println(string0.contentEquals("abcdef"));
結果:true
15 contentEquals(StringBuffer sb)
將此字符串與指定的 StringBuffer比。相同,true,否則:false
String string0 ="abcdef";
System.out.println( string0.contentEquals(newStringBuffer("abcdef")));
結果:true
16 getChars(int srcBegin, int srcEnd, char[] dst, intdstBegin)
將當前字符串的部分字符復制到目標自負數組dst中,從srcBegin(包含在內)到srcEnd(不包含在內)之間的字符復制到目標字符數組中的字符從dstBegin位置開(kāi)始存放.
String ss="hello,word";
char dst[]={'a','b','c','d','e','f'};
ss.getChars(4,6,dst,3);
System.out.println(dst);
結果abco,f
17.getBytes(String charsetName);
String ss = "abcde";
byte dst[] = new byte[5];
try {
dst = ss.getBytes("GB2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
for (int i = 0; i < dst.length; i++) {
System.out.print(i==4?dst[i]:dst[i]+",");
}
}
結果:97,98,99,100,101
18 getBytes
String ss = "abcde";
byte dst[] = new byte[5];
dst = ss.getBytes();
for (int i = 0; i < dst.length; i++) {
System.out.print(i==4?dst[i]:dst[i]+",");
}
結果:97,98,99,100,101
startsWith(String perfix) 是否以perfix開(kāi)頭,yes返回true ,no返回false
String string ="abcbd";
System.out.println( string.startsWith("abc"));
System.out.println( string.startsWith("Ab"));
結果:true
false
startsWith(Stringprefix, inttoffset)偏移toffset,是否以perfix開(kāi)頭,yes返回true ,no返回false
String string ="abcde";
System.out.println( string.startsWith("cd", 2));
包含toffset對應的字符串
結果:true
endsWith(Stringsuffix)是否以suffix結尾,yes 返回true,no返回false
String string ="abcde";
System.out.println( string.endsWith("e"));
結果:true
trim()去掉字符串的前后空格
String string =" abc ";
System.out.println(string.length()+","+ string.trim().length());
結果:5,3