Java String 常用方法,涵蓋全部

1、[/size]

[size=5]

[/size]

[size=5]String類常用方法

2

1。2、求字串某一位置字元

public char charAt(int index)//返回字串中指定位置的字元;注意字串中第一個字元索引是0,最後一個是length()-1。

String str = new String(“asdfzxc”);

char ch = str。charAt(4);//ch = z12

1

2

1。3、提取子串

用String類的substring方法可以提取字串中的子串,該方法有兩種常用引數:

1)public String substring(int beginIndex)//該方法從beginIndex位置起,從當前字串中取出剩餘的字元作為一個新的字串返回。

2)public String substring(int beginIndex, int endIndex)//該方法從beginIndex位置起,從當前字串中取出到endIndex-1位置的字元作為一個新的字串返回。

String str1 = new String(“asdfzxc”);

String str2 = str1。substring(2);//str2 = “dfzxc”

String str3 = str1。substring(2,5);//str3 = “dfz” 123

1

2

3

1。4、字串比較

1)public int compareTo(String anotherString)//該方法是對字串內容按字典順序進行大小比較,透過返回的整數值指明當前字串與引數字串的大小關係。若當前物件比引數大則返回正整數,反之返回負整數,相等返回0。

2)public int compareToIgnore(String anotherString)//與compareTo方法相似,但忽略大小寫。

3)public boolean equals(Object anotherObject)//比較當前字串和引數字串,在兩個字串相等的時候返回true,否則返回false。

4)public boolean equalsIgnoreCase(String anotherString)//與equals方法相似,但忽略大小寫。

String str1 = new String(“abc”);

String str2 = new String(“ABC”);

int a = str1。compareTo(str2);//a>0

int b = str1。compareTo(str2);//b=0

boolean c = str1。equals(str2);//c=false

boolean d = str1。equalsIgnoreCase(str2);//d=true123456

1

2

1。5、字串連線

public String concat(String str)//將引數中的字串str連線到當前字串的後面,效果等價於”+”。

String str = “aa”。concat(“bb”)。concat(“cc”); 1

1

相當於String str = “aa”+”bb”+”cc”;

1。6、字串中單個字元查詢

1)public int indexOf(int ch/String str)//用於查詢當前字串中字元或子串,返回字元或子串在當前字串中從左邊起首次出現的位置,若沒有出現則返回-1。

2)public int indexOf(int ch/String str, int fromIndex)//

方法與第一種類似,區別在於該方法從fromIndex位置向後查詢。

3)public int lastIndexOf(int ch/String str)//該方法與第一種類似,區別在於該方法從字串的末尾位置向前查詢。

4)public int lastIndexOf(int ch/String str, int fromIndex)//該方法與第二種方法類似,區別於該方法從fromIndex位置向前查詢。

String str = “I am a good student”;

int a = str。indexOf(‘a’);//a = 2

int b = str。indexOf(“good”);//b = 7

int c = str。indexOf(“w”,2);//c = -1

int d = str。lastIndexOf(“a”);//d = 5

int e = str。lastIndexOf(“a”,3);//e = 2123456

1

2

3

1。7、字串中字元的大小寫轉換

1)public String toLowerCase()//返回將當前字串中所有字元轉換成小寫後的新串

2)public String toUpperCase()//返回將當前字串中所有字元轉換成大寫後的新串

String str = new String(“asDF”);

String str1 = str。toLowerCase();//str1 = “asdf”

String str2 = str。toUpperCase();//str2 = “ASDF” 123

1

2

3

1。8、字串中字元的替換

1)public String replace(char oldChar, char newChar)//用字元newChar替換當前字串中所有的oldChar字元,並返回一個新的字串。

2)public String replaceFirst(String regex, String replacement)//該方法用字元replacement的內容替換當前字串中遇到的第一個和字串regex相匹配的子串,應將新的字串返回。

3)public String replaceAll(String regex, String replacement)//該方法用字元replacement的內容替換當前字串中遇到的所有和字串regex相匹配的子串,應將新的字串返回。

String str = “asdzxcasd”;

String str1 = str。replace(‘a’,‘g’);//str1 = “gsdzxcgsd”

String str2 = str。replace(“asd”,“fgh”);//str2 = “fghzxcfgh”

String str3 = str。replaceFirst(“asd”,“fgh”);//str3 = “fghzxcasd”

String str4 = str。replaceAll(“asd”,“fgh”);//str4 = “fghzxcfgh” 12345

1

2

3

String str = “ a sd ”;

String str1 = str。trim();

int a = str。length();//a = 6

int b = str1。length();//b = 4 1234

1

2

3

4

//用來比較當前字串的起始字元或子字串prefix和終止字元或子字串suffix是否和當前字串相同,過載方法中同時還可以指定比較的開始位置offset。

2)boolean statWith(String prefix)或boolean endWith(String suffix)

String str = “asdfgh”;

boolean a = str。statWith(“as”);//a = true

boolean b = str。endWith(“gh”);//b = true 123

1

2

3//從當前字串的firstStart位置開始比較,取長度為length的一個子字串,other字串從otherStart位置開始,指定另外一個長度為length的字串,兩字串比較,當b為true時字串不區分大小寫。

3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)

4)contains(String str)//判斷引數s是否被包含在字串中,並返回一個

布林

型別的值。

String str = “student”;

str。contains(“stu”);//true

str。contains(“ok”);//false 123

1

2

3

5)String[] split(String str)//將str作為分隔符進行字串分解,分解後的字字串在字串陣列中返回。

String str = “asd!qwe|zxc#”;

String[] str1 = str。split(“!|#”);//str1[0] = “asd”;str1[1] = “qwe”;str1[2] = “zxc”; 12

1

2

3

indexOf(String str):獲取str在字串物件中第一次出現的索引

String str = “adsfaxsdfas沙發上案發地方”;

System。out。println(str。indexOf(‘a’,5));

1

2

執行結果:

94)substring(int start):從start開始擷取字串

String str = “adsfaxsdfas沙發上案發地方”;

System。out。println(str。substring(1));

1

2

執行結果:

dsfaxsdfas沙發上發地方

5)String substring(int start,int end):從start開始,到end結束擷取字串。包括start,不包括

1endString str = “adsfaxsdfas沙發上案發地方”;

System。out。println(str。substring(1, 12));

2

執行結果:

dsfaxsdfas沙

String判斷功能1)[/size]

[size=5]

equals(Object obj):比較字串的內容是否相同

String str = “adsfaxsdfas沙發上案發地方”;

1System。out。println(str。equals(“adsfaxsdfas沙發上發地方”));

2System。out。println(str。equals(“adsfaxsdfas”));

3

執行結果:

true

false2)

equalsIgnoreCase(String anotherString):比較字串的內容是否相同,忽略大小寫

1String str = “adsfaxsdfas沙發上案發地方”;

2System。out。println(str。equalsIgnoreCase(“ADsfaxsdfAs沙發上發地方”));

執行結果:

true3)

startsWith(String prefix):判斷字串物件是否以指定的字元開頭(區分大小寫)

2String str = “adsfaxsdfas沙發上案發地方”;

System。out。println(str。startsWith(“a”));

System。out。println(str。startsWith(“A”));

1

3

執行結果:

true

false4)

startsWith(String prefix,int toffset):判斷字串物件是否以指定的字元開頭,引數toffset為指定從哪個下標開始

String str = “adsfaxsdfas沙發上案發地方”;

System。out。println(str。startsWith(“f”, 3));

System。out。println(str。startsWith(“f”, 4));

1

2

3

執行結果:

true

false5)

endsWith(String str):判斷字串物件是否以指定的字元結尾

String str = “adsfaxsdfas沙發上案發地方”;

System。out。println(str。endsWith(“x”));

System。out。println(str。endsWith(“方”));

1

2

3

執行結果:

false

true6)

isEmpty():判斷指定字串是否為空7)startsWith(String prefix):判斷字串是否以指定字元開頭

String str = “adsfaxsdfas沙發上發地方”;

System。out。println(str。startsWith(“a”));

1

2

執行結果:

true4。

toLowerCase():把字串轉換為小寫字串

String str1 = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

1System。out。println(str1。toLowerCase());

2

執行結果:

abcdefghijklmnopqrstuvwxyz3)

toUpperCase():把字串轉換為大寫字串

String str1 = “abcdefghijklmnopqrstuvwxyz”;

System。out。println(str2。toUpperCase());

1

2

執行結果:

ABCDEFGHIJKLMNOPQRSTUVWXYZ5。

其他常用方法1)trim():去除字串兩端空格

String str3 = “ a c e x u a n x u a n ”;

1

2System。out。println(str3。trim());System。out。println(str3);

3執行結果:a c e x u a n x u a n a c e x u a n x u a n

6

7

2)split():去除字串中指定的的字元,然後返回一個新的字串

public static void main(String[ args) {

String str = “adsfaxsdfas沙發上發地方”;

String array[ = str。split(“a”);

printString(array);

}

public static void printString(String a[) {

for(int i=0;i

System。out。print(a[i); }

}

1

2執行結果:

dsfxsdfs沙發上發地方

3)subSequence(int beginIndex,int endIndex ):擷取字串中指定位置的字元組成一個新的字串

String str = “adsfaxsdfas沙發上發地方”;

System。out。println(str。subSequence(1, 10));

1

2

執行結果:

dsfaxsdfa

4)replace(char oldChar, char newChar):將指定字元替換成另一個指定的字元

String str = “adsfaxsdfas沙發上發地方”;

System。out。println(str。replace(‘a’, ‘s’));

1

2

執行結果:

sdsfsxsdfss沙發上發地方

5)replaceAll(String regex,String replasement):用新的內容替換全部舊內容

String str4 = “Hello,world!”;

System。out。println(str4。replaceAll(“l”, “&”));

1

2

執行結果:

He&&o,wor&d!

6)replaceFirst(String regex,String replacement):替換首個滿足條件的內容

String str = “adsfaxsdfas沙發上發地方”;

System。out。println(str。replaceFirst(“沙”, “璇”));

1

2

執行結果:

adsfaxsdfas璇發上發地方

7)lastIndexOf(String str):返回指定字元出現的最後一次的下標

String str4 = “Hello,world!”;

System。out。println(str4。lastIndexOf(“l”));

1

2

執行結果:

9

8)contains(CharSequence s):檢視字串中

是都

含有指定字元

String str4 = “Hello,world!”;

System。out。println(str4。contains(“l”));

1

2執行結果:

true

[/size]

[size=5][b]9)concat(String str):在原有的字串的基礎上加上指定字串[/b][/size]

[size=5]String str5 [color=rgb(98, 114, 164)]=[/color] [color=rgb(241, 250, 140)]“dr”[/color][color=rgb(153, 153, 153)];[/color][/size]

[size=5]System[color=rgb(153, 153, 153)]。[/color]out[color=rgb(153, 153, 153)]。[/color][color=rgb(139, 233, 253)]println[/color][color=rgb(153, 153, 153)]([/color]str5[color=rgb(153, 153, 153)]。[/color][color=rgb(139, 233, 253)]concat[/color][color=rgb(153, 153, 153)]([/color][color=rgb(241, 250, 140)]“eam”[/color][color=rgb(153, 153, 153)])[/color][color=rgb(153, 153, 153)])[/color][color=rgb(153, 153, 153)];[/color][/size]

[size=5]

[/size]

[size=5]2。2、基本型別轉換為字串型別

String類中提供了String valueOf()

放法

,用作基本型別轉換為字串型別。

1)static String valueOf(char data[])

2)static String valueOf(char data[], int offset, int count)

3)static String valueOf(boolean b)

4)static String valueOf(char c)

5)static String valueOf(int i)

6)static String valueOf(long l)

7)static String valueOf(float f)

8)static String valueOf(double d)

例如:

String s1 = String。valueOf(12);

String s1 = String。valueOf(12。34);