1.返回string“長度”方法
你如何確定給定string的長度?java提供了一種稱為“length()”的方法。將它用于您需要查找string的長度。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class str_sample { public static void main(string[] args){ //測試string長度的方法 string name= "hello work" ; //length方法返回的是整數 int num=name.length(); system.out.println( "字符串的長度:" +num); } } |
輸出結果:
字符串長度:10
2.字符串“indexof()”方法
我怎么能找到哪個角色在哪個位置?
indexof”可以幫助你指定的特定字符第一次出現的位置,若找不到返回-1
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; int num=name.indexof( "java" ); system.out.println( "java第一次出現的位置:" +num); } } |
輸出結果:
java第一次出現的位置:7
3.字符串“lastindexof()”方法
如果我知道長度,我想從字符串后面查找角色在哪個位置?
“lastindexof”可以從指定位置開始反向檢索,返回最后出現你指定的特定字符的位置,若找不到返回-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; //name的長度 int num=name.length(); //lastindexof(指定特殊字符,指定位置) int index=name.lastindexof( "java" ,num); system.out.println( "java最后一次出現的位置:" +index); } ] |
輸出結果:
java最后一次出現的位置:29
4.字符串“substring()”方法
如果我只想要字符串中的一段,那我該怎么辦呢?
“substring”可以從指定的頭和為截取字符串,返回截取后的字符串。注意:java中表示范圍都是含頭不含尾。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; //截取how這個字母,首先你要知道h的下標,可使用之前的indexof方法 int num=name.indexof( "h" ); //由于java中示范圍都是含頭不含尾,所以要多加一位 string str=name.substring(num,num+ 3 ); system.out.println(str); //也可從指定位置直接截取到字符串尾部 string str2=name.substring(num); system.out.println(str2); } ] |
輸出結果:
how
how do you like java?
5.字符串“charat()”方法
怎么能根據位置獲取字符呢?
“chatat”可以幫到你,用于返回指定下標的字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; //創建循環遍歷name的下標 for ( int i= 0 ;i<name.length();i++){ //將下標放入charat方法中 char ch=name.charat(i); system.out.print(ch); } } ] |
輸出結果:
i like java. how do you like java?
6.字符串“startswith(),endswith()”方法
怎么判斷字符串是以什么開頭或什么結束的呢?
“startswith()”,檢查字符串是否以指定字符串開始。“endswith()”檢查字符串是否以指定字符串結尾
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; //是否以“i”開頭,正確返回true,否則返回false boolean s1=name.startswith( "i" ); //startswith()第二種用法,可判斷指定位置是否是指定字符串 boolean s2=name.startswith( "java" , 7 ); //判斷字符串是否以“?”結尾 boolean e1=name.endswith( "?" ); system.out.println( "是否以“i”開頭:" +s1); system.out.println( "位置7是否是“java”開頭:" +s2); system.out.println( "是否以“?”結尾:" +e1); } } |
輸出結果:
否以“i”開頭:true
位置7是否是“java”開頭:true
是否以“?”結尾:true
7.字符串“compareto()”方法
"compareto"它從第一位開始比較, 如果遇到不同的字符,則馬上返回這兩個字符的ascii值差值.返回值是int類型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class str_sample{ public static void main(string[] args){ //a的ascli值為65,a的ascli值為97 string a= "a" ; string b= "a" ; string c= "aa" ; string d= "abc" ; string e= "ad" ; int num=a.compareto(b); //還有一種方法忽略大小寫進行比較 int num2=a.comparetoignorecase(b); //長度不一樣且前幾個字符也不一樣,從第一位開始找,當找到不一樣的字符時,則返回的值是這兩個字符比較的值 int num3=c.compareto(d); //如多個字符,第一個字符相同則直接比較第二個字符,以此類推 int num4=e.compareto(c); system.out.println( "a與b比較:" +num); system.out.println( "a與b比較(忽略大小寫):" +num2); system.out.println( "c與d比較:" +num3); system.out.println( "e與d比較:" +num4); } } |
輸出結果:
a與b比較:-32
a與b比較(忽略大小寫):0
c與d比較:-1
e與d比較:3
8.字符串“contains()”方法
如果你想知道字符串中是否包含你想要的字符串?
那么“contanins”可以滿足你的需求,判斷是否包含指定字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; //判斷是否包含“you”這個字符串 boolean bl=name.contains( "you" ); system.out.println( "name字符串中是否包含“you”:" +bl); } ] |
輸出結果:
name字符串中是否包含“you”:true
9.字符串“replace()”方法
您可以指定要替換的字符串部分以及參數中的替換字符串。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; string str=name.replace( "java" , "php" ); system.out.println( "替換前:" +name); system.out.println( "替換后:" +str); } ] |
輸出結果:
替換前:i like java. how do you like java?
替換后:i like php. how do you like php?
10.字符串“tolowercase()”和“touppercase()”方法
“tolowercase()”將字符串以小寫形式顯示,touppercase()”將字符串以大寫形式顯示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class str_sample{ public static void main(string[] args){ string name= "i like java. how do you like java?" ; string low=name.tolowercase(); string upp=name.touppercase(); system.out.println( "小寫顯示:" +low); system.out.println( "大寫顯示:" +upp); } ] |
輸出結果:
小寫顯示:i like java. how do you like java?
大寫顯示:i like java. how do you like java?