小豬學(xué)習(xí)

小小白慢慢學(xué)習(xí)中ing 第二十天
努力努力
本日內(nèi)容(String 類)
能夠表示字符串:String類,StringBuffer類,StringBuilder類。
String類:字符串內(nèi)容不可以改變。
StringBuffer和StringBuilder:內(nèi)容可以改變。
1、String類:內(nèi)容不能更改
2.1.1 字符串常量
字符串的數(shù)據(jù)是常量,存儲在字符串池中,字符串的數(shù)值是不能更改的。字符串池中不允許存儲重復(fù)的字符串。因為復(fù)用性。
字符串:0-多個字符的序列。使用雙引號引起來的內(nèi)容。
"",字符串對象存在,但是里面沒有字符
"a",字符串中有一個字符
"abc",
"hello world",
注意:'a'和"a"
char c = 'a';//基本數(shù)據(jù)類型
String s = "a";//引用類型,String類的
2.1.2 null和" "
null,是指字符串對象壓根就不存在,內(nèi)存中沒有
" ",空字符串:字符串對象存在的,開辟內(nèi)存,但是內(nèi)容是空的。
2.1.3 字符串下標(biāo)
字符串中的每個字符,其實都有固定的下標(biāo),index,從0開始,到長度減1。超出范圍就下標(biāo)越界。同數(shù)組類似。
2.1.4 String類的常用方法
1、創(chuàng)建字符串:
????????//1.直接創(chuàng)建
????????????????String s1 = "abc";
????????//2.通過構(gòu)造方法
????????????????String s2 = new String();//""
????????//3.其他的構(gòu)造方法
????????????????new String("abc");
????????????????new String(byte[]);
????????????????new String(byte[] ,offset ,length);
????????????????new String(char[])
????????????????new String(char[] ,offset,count);
? ? ? ? //....
2、常用方法:
A:搜索類
????????indexOf(int)-->int
????????indexOf(String)-->int
????????indexOf(int ,fromIndex)-->int
????????indexOf(String,fromIndex)-->int
?
????????lastIndexOf(int)
????????lastIndexOf(String)
????????lastIndexOf(int,fromIndex)
????????lastIndexOf(String,fromIndex)

B:判斷類
????????contains()-->boolean,是否包含指定內(nèi)容
????????equals()-->boolean,判斷字符串的字面值,是否相等,重寫Object類
????????equalsIgnoreCase()-->boolean,忽略大小寫的比較
????????startsWith()-->boolean,是否以指定內(nèi)容開頭
????????endsWith()-->boolean,是否以指定內(nèi)容結(jié)尾

C:獲取新的字符串
????????concat()-->String,拼接,作用同+
????????subString(fromIndex)--->String,截取子串

? ? ? ? subString(fromIndex,endIndex)-->String,包含fromIndex,不包含endIndex
????????replace(oldchar,newchar)-->String,替換
????????replace(CharSequence,CharSequence)-->String
????????trim()->String,忽略首尾空格
????????toLowerCase()-->String,轉(zhuǎn)小寫
????????toUpperCase()-->String,轉(zhuǎn)大寫

D:獲取其他
length()-->int,獲取長度
getBytes()-->byte[],獲取字節(jié)數(shù)組
toCharArray()-->char[],獲取字符數(shù)組
charAt(index)-->char,根據(jù)下標(biāo)獲取指定的字符
split(切割符)-->String[],切割

