黑马程序员-4 String类和StringBuffer类
------- android培訓、java培訓、期待與您交流! ----------
?
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在字符串中出現的位置。
int lastIndexOf(int ch) :
2,判斷。
2.1 字符串中是否包含某一個子串。
boolean contains(str):
特殊之處:indexOf(str):可以索引str第一次出現位置,如果返回-1.表示該str不在字符串中存在。
所以,也可以用于對指定判斷是否包含。
if(str.indexOf("aa")!=-1)
而且該方法即可以判斷,有可以獲取出現的位置。
2.2 字符中是否有內容。
boolean isEmpty(): 原理就是判斷長度是否為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)
//3+"";//String.valueOf(3);
特殊:字符串和字節數組在轉換過程中,是可以指定編碼表的。
4,替換
String replace(oldchar,newchar);
5,切割
String[] split(regex);
6,子串。獲取字符串中的一部分。
String substring(begin);
String substring(begin,end);
7,轉換,去除空格,比較。
7.1 將字符串轉成大寫或則小寫。
String toUpperCase();
String toLowerCase();
7.2 將字符串兩端的多個空格去除。
String trim();
7.3 對兩個字符串進行自然順序的比較。
int compareTo(string);
?
?
?
?
StringBuffer是字符串緩沖區。
是一個容器。
特點:
1,長度是可變化的。
2,可以字節操作多個數據類型。
3,最終會通過toString方法變成字符串。
?
C create U update R read D delete
1,存儲。
StringBuffer append():將指定數據作為參數添加到已有數據結尾處。
StringBuffer insert(index,數據):可以將數據插入到指定index位置。
2,刪除。
StringBuffer delete(start,end):刪除緩沖區中的數據,包含start,不包含end。
StringBuffer deleteCharAt(index):刪除指定位置的字符。
3,獲取。
char charAt(int index)
int indexOf(String str)
int lastIndexOf(String str)
int length()
String substring(int start, int end)
4,修改。
StringBuffer replace(start,end,string);
void setCharAt(int index, char ch) ;
5,反轉。
StringBuffer reverse();
6,
將緩沖區中指定數據存儲到指定字符數組中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
JDK1.5 版本之后出現了StringBuilder.
StringBuffer是線程同步。
StringBuilder是線程不同步。
以后開發,建議使用StringBuilder
升級三個因素:
1,提高效率。
2,簡化書寫。
3,提高安全性。
轉載于:https://www.cnblogs.com/grjelf/archive/2012/10/18/2726499.html
總結
以上是生活随笔為你收集整理的黑马程序员-4 String类和StringBuffer类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle spatial 周边查询S
- 下一篇: 线程Thread,Runnable