------- android培訓、java培訓、期待與您交流! ----------
String的操作:
1獲取
1.1 字符串中的包含的字符數,也就是字符串的長度。
int length();獲取字符串的長度。
1.2 根據位置獲取位置上的某個字符。
char charAt(int index);
1.3 根據字符獲取該字符串中的位置。
int indexOf(int ch);返回的事ch在字符串中第一次出現的位置。
int indexOf(int ch,int fromIndex);從fromIndex指定位置開始。獲取ch在字符串出現的位置。
int indexOf(String str);返回的事str在字符串中第一次出現的位置。
int indexOf(String str,int fromIndex);從fromIndex指定位置開始。獲取str在字符串出現的位置。
2判斷
2.1 字符串中是否包含某一個子串。
boolean contains();
2.2 字符串是否有內容。
boolean isEmpty(str);//判斷長度是否爲0
2.3 字符串是否是以指定內容開頭。
boolean startsWith(str);
2.4 字符串是否是以指定內容結尾。
boolean endsWith(str);
2.5 判斷字符串是否項目,複寫了Object類的中equals方法
boolean equals(str);
2.6 判斷內容是否相同,並忽略大小寫。
boolean equalsIgnoreCase();
3轉換
3.1 將字符數組轉成字符串。
構造函數:
String(char[])
String(char[],offset,count);//將字符數組中的一部分轉成一個字符串。
靜態方法:
static String copyValueOf(char[]);
static String copyValueOf(char[] data,int offset,int count);
static String valueOf(char[]);
3.2將字符串轉換數組
char[] toCharArray();
3.3將字節數數組轉成字符串
String(byte[])
String(byte[],offset,count);將字節數組的一部分轉換爲一個字符串。
3.4 將字符串轉換成字節數組
byte[] getBytes();
3.5 將基本數據類型轉換字符串
static String valueOf(int);
static String valueOf(double);
特殊:字符串和字節數組在轉換過程中,是可以指定編碼表的。
4替換
String replace(oldchar,newchar);
5切割
String[] split(regex);
6子串 截取字符串中的一部分
String substring(beginIndex);
String substring(beginIndex,endIndex);包含頭不包含尾
7 字符串轉換 去除空格 比較
7.1 將字符串轉換大寫或小寫
String toUpperCase();
String toLowerCase();
7.2 將字符串兩端的多個空格去除
String trim();
7.3對兩個字符串進行自然順序的比較。
int compareTo(String);