Java String類淺析

String類被final修飾,所以不能被繼承。該類實現了 java.io.Serializable, Comparable<String>,CharSequence接口

構造方法:

               public String(); // 將value初始化爲一個長度爲0的字符數組

               public String(String original); //用一個字符串構造一個字符串實例

               public String(char value[ ]) ; //用字符數組構造一個字符串,實質是將字符串內容複製給value

               public String(char value[ ], int offset, int count); // 用字符數組的一部分構造一個字符串

public String(int[]codePoints,intoffset,intcount);//用ASCII碼數組構造一個字符串

public String(byteascii[],inthibyte,intoffset,intcount);hibyte是每個16位unicode的高8位

public String(byteascii[],inthibyte)

public String(bytebytes[],intoffset,intlength,StringcharsetName);//charsetName爲編碼名稱

public String(bytebytes[],intoffset,intlength,Charsetcharset) ;

public String(bytebytes[],StringcharsetName);

public String(bytebytes[],Charsetcharset);

public String(bytebytes[],intoffset,intlength);

public String(bytebytes[]);

public String(StringBufferbuffer);

public String(StringBuilderbuilder) ;

私有域: private  final char value[ ]  //String實質上是一個字符數組

               private int hash; //存儲字符串的HashCode

public方法:

 靜態:

public static String format(Stringformat,Object... args);//返回指定格式的字符串

public static String valueOf(Objectobj);//返回obj.toString();

public static String valueOf(chardata[]); //返回data字符串數組對應的字符串

public static String valueOf(chardata[],int offset, int count) ;//返回data字符數組指定偏移量的長度爲count的字符串

public static String valueOf(booleanb);//將Boolean類型的值轉化成“true”或者“false”;

public static String valueOf(charc);//將單個字符轉換成字符串

public static String valueOf(inti);//將整數轉換成字符串

public static String valueOf(longl);//將長整型轉換成字符串

public static String valueOf(floatf);//將小數轉換成字符串

public static String valueOf(doubled);//將小數轉換成字符串

非靜態:

public int length() ; //返回字符串長度,實質是return value.length;

public boolean isEmpty() ;//判斷字符串是否爲空,實質是判斷value長度是否爲0

public char charAt(intindex);//求某個指定位置的字符,實質是value[index]

public int codePointAt(intindex);//返回指定位置字符的編碼,實質是Character.codePointAtImpl(value, index, value.length);

public int codePointBefore(intindex);//返回指定位置之前的字符編碼,也是對value[] 操作

public int codePointCount(intbeginIndex,intendIndex);//返回範圍之內的unicode point數量

public int offsetByCodePoints(intindex,intcodePointOffset);通過codePointOffset指定偏移量

public void getChars(intsrcBegin,intsrcEnd,chardst[],intdstBegin);//將字符串指定範圍內的字符複製到目的字符數組

public void getBytes(intsrcBegin,intsrcEnd,bytedst[],intdstBegin);//複製到目的byte數組,只複製低8位,8八位丟棄

public byte[] getBytes(StringcharsetName);//通過指定的編碼格式將該字符串轉換成對應的byte數組存儲

public byte[] getBytes(Charsetcharset);//同上

public byte[] getBytes();//通過平臺默認的編碼格式轉換字符串到一個byte數組裏

public boolean equals(ObjectanObject);//比較兩個字符串是否相同

public boolean contentEquals(StringBuffersb) ;//同StringBuffer的內容比較是否相同

public boolean contentEquals(CharSequencecs);//同CharSequence的內容比較是否相同

public boolean equalsIgnoreCase(StringanotherString); //同另一個字符串比較,忽略大小寫

public int compareTo(StringanotherString);//與另一個字符串比較,如果該字符串是anotherString字符串的前綴,則返回value.length-anotherString.length;

如果anotherString是該字符串的前綴,則返回anotherString.length-value.length;

否則返回兩個字符串從頭開始第一個不一樣的字符之差

public int compareToIgnoreCase(Stringstr) ;//與str比較,忽略大小寫

                                           

public boolean regionMatches(inttoffset,Stringother,intooffset,int len);//比較兩個字符串的子串,toffset指定本字符串的偏移量,other指定比較的另一個字符串,ooffset指定Other的偏移量,len指定比較的長度

public boolean regionMathces(boolean ignoreCase,int toffset,String other,int ooffset,int len);//同上,指定了是否忽略大小寫

public boolean startsWith(Stringprefix,inttoffset);//判斷prefix是否是從指定位置起的字串的前綴

public boolean startsWith(Stringprefix);//同上,默認toffset爲0

public boolean endsWith(Stringsuffix) ;//suffix是否是該字符串的結尾部分

public int indexOf(intch);//返回指定字符(ch)在該字符串中第一次出現的位置

public int indexOf(intch,intfromIndex);//同上,但是指定了搜索的起始位置

public int lastIndexOf(intch,intfromIndex);//返回ch最後出現的位置,從fromIndex往前搜索

public int lastIndexOf(intch);//返回ch最後出現的位置,從value.length-1往前搜索

public int indexOf(Stringstr,intfromIndex);//返回指定位置開始出現的str的起始位置

public int indexOf(Stringstr) ;//返回從頭開始出現的str的起始位置

public int lastIndexOf(Stringstr);//返回str最後出現的位置

public int lastIndexOf(Stringstr,intfromIndex)//從fromindex往前搜索,返回str最後出現的位置

public String substring(intbeginIndex);//從指定位置開始的字串

public String substring(intbeginIndex,intendIndex) ;//指定範圍內的字串

public CharSequence subSequence(intbeginIndex,intendIndex);//指定範圍內的字符序列

public String concat(Stringstr);//在字符串後面再連接一個字符串

public String replace(charoldChar,charnewChar);// 將字符串中的oldChar全部替換成newChar並返回新的字符串

public boolean matches(Stringregex);//是否滿足正則表達式

public boolean contains(CharSequences);//是否包含s

public String replaceFirst(Stringregex,Stringreplacement) ;//將第一個滿足正則表達式的字串替換成replacement

public String replaceAll(Stringregex,Stringreplacement);//將所有滿足正則表達式的子串替換成replacement

public String replace(CharSequencetarget,CharSequencereplacement) ;//將target字串替換成replacement

public String[] split(Stringregex,intlimit);//分割字符串,limit指定起始位置,遇到regex則進行分割

public String[] split(Stringregex) ;//分割字符串,limit=0;

public String toLowerCase(Localelocale);//通過規則locale轉成小寫,映射基於unicode編碼,由於字符映射並非是1:1 的,所以結果字符串的長度可能與被轉的字符串長度不一致

public String toLowerCase();//localse爲默認值

public String toUpperCase(Localelocale) ;//轉大寫

public String toUpperCase();//locale爲默認值

public String trim();//去掉首位的空白符

public char[] toCharArray();// 返回字符數組



發佈了25 篇原創文章 · 獲贊 4 · 訪問量 9328
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章