能夠表示字符串:String類耗帕,StringBuffer類劲阎,StringBuilder類袁串。
- String類:字符串內(nèi)容不可以改變管呵。
- StringBuffer和StringBuilder:內(nèi)容可以改變梳毙。
1.1、String類:內(nèi)容不能更改
1.1.1 字符串常量
字符串的數(shù)據(jù)是常量捐下,存儲(chǔ)在字符串池中账锹,字符串的數(shù)值是不能更改的。字符串池中不允許存儲(chǔ)重復(fù)的字符串坷襟。因?yàn)閺?fù)用性奸柬。
字符串:0-多個(gè)字符的序列。使用雙引號(hào)引起來(lái)的內(nèi)容婴程。
""廓奕,字符串對(duì)象存在,但是里面沒(méi)有字符
"a"档叔,字符串中有一個(gè)字符
"abc"桌粉,
"hello world",
注意:'a'和"a"
char c = 'a';//基本數(shù)據(jù)類型
String s = "a";//引用類型衙四,String類的
1.1.2 null和""
null铃肯,是指字符串對(duì)象壓根就不存在,內(nèi)存中沒(méi)有
""传蹈,空字符串:字符串對(duì)象存在的押逼,開辟內(nèi)存,但是內(nèi)容是空的惦界。
1.1.3 字符串下標(biāo)
字符串中的每個(gè)字符挑格,其實(shí)都有固定的下標(biāo),index沾歪,從0開始漂彤,到長(zhǎng)度減1。超出范圍就下標(biāo)越界灾搏。同數(shù)組類似挫望。
1.1.4 String類的常用方法
1、創(chuàng)建字符串:
//1.直接創(chuàng)建
String s1 = "abc";
//2.通過(guò)構(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仰冠,獲取長(zhǎng)度
getBytes()-->byte[],獲取字節(jié)數(shù)組
toCharArray()-->char[]乏冀,獲取字符數(shù)組
charAt(index)-->char,根據(jù)下標(biāo)獲取指定的字符
split(切割符)-->String[]洋只,切割