Java基礎十三

Java基礎

String類

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在字符串中出現的位置。

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

字符串的組成原理就是通過該類實現的。

StringBuffer可以對字符串內容進行增刪。

StringBuffer是一個容器。

很多方法與String相同。

StingBuffer是可變長度的。

StringBuffer是字符串緩衝區。
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,提高安全性。

基本數據類型對象包裝類

byte Byte
short Short
int Integer
long Long
boolean Boolean
float Float
double Double
char Character

基本數據類型對象包裝類的最常見作用,
就是用於基本數據類型和字符串類型之間做轉換

基本數據類型轉成字符串。

基本數據類型+" "

基本數據類型.toString(基本數據類型值);

如: Integer.toString(34);//將34整數變成"34";

字符串轉成基本數據類型。

xxx a = Xxx.parseXxx(String);

int a = Integer.parseInt(“123”);

double b = Double.parseDouble(“12.23”);

boolean b = Boolean.parseBoolean(“true”);

Integer i = new Integer(“123”);

int num = i.intValue();

十進制轉成其他進制。
toBinaryString();
toHexString();
toOctalString();

其他進制轉成十進制。
parseInt(string,radix);

基本數據類型對象包裝類新特性

JDK1.5以後,簡化了定義方式。
• Integer x = new Integer(4);可以直接寫成
• Integer x = 4;//自動裝箱。
• x = x + 5;//自動拆箱。通過intValue方法。

需要注意:
在使用時,Integer x = null;上面的代碼就會出現 NullPointerException。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章