本文實例講述了java string類功能、原理與應用。分享給大家供大家參考,具體如下:
string構造方法
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package cn.itcast_01; /* * 字符串:就是由多個字符組成的一串數據。也可以看成是一個字符數組。 * 通過查看api,我們可以知道 * a:字符串字面值"abc"也可以看成是一個字符串對象。 * b:字符串是常量,一旦被賦值,就不能被改變。 * * 構造方法: * public string():空構造 * public string(byte[] bytes):把字節數組轉成字符串 * public string(byte[] bytes,int index,int length):把字節數組的一部分轉成字符串 * public string(char[] value):把字符數組轉成字符串 * public string(char[] value,int index,int count):把字符數組的一部分轉成字符串 * public string(string original):把字符串常量值轉成字符串 * * 字符串的方法: * public int length():返回此字符串的長度。 */ public class stringdemo { public static void main(string[] args) { // public string():空構造 string s1 = new string(); system.out.println( "s1:" + s1); system.out.println( "s1.length():" + s1.length()); system.out.println( "--------------------------" ); //s1: //s1.length():0 // public string(byte[] bytes):把字節數組轉成字符串 byte [] bys = { 97 , 98 , 99 , 100 , 101 }; string s2 = new string(bys); system.out.println( "s2:" + s2); //abcde system.out.println( "s2.length():" + s2.length()); // system.out.println( "--------------------------" ); // public string(byte[] bytes,int index,int length):把字節數組的一部分轉成字符串 // 我想得到字符串"bcd" string s3 = new string(bys, 1 , 3 ); system.out.println( "s3:" + s3); system.out.println( "s3.length():" + s3.length()); system.out.println( "--------------------------" ); // public string(char[] value):把字符數組轉成字符串 char [] chs = { 'a' , 'b' , 'c' , 'd' , 'e' , '愛' , '林' , '親' }; string s4 = new string(chs); system.out.println( "s4:" + s4); system.out.println( "s4.length():" + s4.length()); system.out.println( "--------------------------" ); // public string(char[] value,int index,int count):把字符數組的一部分轉成字符串 string s5 = new string(chs, 2 , 4 ); system.out.println( "s5:" + s5); system.out.println( "s5.length():" + s5.length()); system.out.println( "--------------------------" ); //public string(string original):把字符串常量值轉成字符串 string s6 = new string( "abcde" ); system.out.println( "s6:" + s6); system.out.println( "s6.length():" + s6.length()); system.out.println( "--------------------------" ); //字符串字面值"abc"也可以看成是一個字符串對象。 string s7 = "abcde" ; system.out.println( "s7:" +s7); system.out.println( "s7.length():" +s7.length()); } } |
字符串的特點:一旦被賦值,就不能改變。
但是引用可以改變
1
2
3
4
5
6
7
8
9
10
11
|
package cn.itcast_02; /* * 字符串的特點:一旦被賦值,就不能改變。 */ public class stringdemo { public static void main(string[] args) { string s = "hello" ; s += "world" ; system.out.println( "s:" + s); // helloworld } } |
圖解:
string s = new string("hello")
和 string s = "hello";
的區別?
string字面值對象和構造方法創建對象的區別
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package cn.itcast_02; /* * string s = new string("hello")和string s = "hello";的區別? * 有。前者會創建2個對象,后者創建1個對象。 * * ==:比較引用類型比較的是地址值是否相同 * equals:比較引用類型默認也是比較地址值是否相同,而string類重寫了equals()方法,比較的是內容是否相同。 */ public class stringdemo2 { public static void main(string[] args) { string s1 = new string( "hello" ); string s2 = "hello" ; system.out.println(s1 == s2); // false system.out.println(s1.equals(s2)); // true } } |
圖解:
1
2
3
4
|
string s5 = "hello" ; string s6 = "hello" ; system.out.println(s5 == s6); // 字符串字面量,直接從內存找,所以true system.out.println(s5.equals(s6)); // true |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package cn.itcast_02; /* * 看程序寫結果 * 字符串如果是變量相加,先開空間,在拼接。 * 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否則,就創建。 */ public class stringdemo4 { public static void main(string[] args) { string s1 = "hello" ; string s2 = "world" ; string s3 = "helloworld" ; system.out.println(s3 == s1 + s2); // false。字符串如果是變量相加,先開空間,再拼接。 system.out.println(s3.equals((s1 + s2))); // true system.out.println(s3 == "hello" + "world" ); //true。字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否則,就創建。 system.out.println(s3.equals( "hello" + "world" )); // true // 通過反編譯看源碼,我們知道這里已經做好了處理。 // system.out.println(s3 == "helloworld"); // system.out.println(s3.equals("helloworld")); } } |
string類的判斷功能:
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
42
43
44
45
46
47
48
49
|
package cn.itcast_03; /* * string類的判斷功能: * boolean equals(object obj):比較字符串的內容是否相同,區分大小寫 * boolean equalsignorecase(string str):比較字符串的內容是否相同,忽略大小寫 * boolean contains(string str):判斷大字符串中是否包含小字符串 * boolean startswith(string str):判斷字符串是否以某個指定的字符串開頭 * boolean endswith(string str):判斷字符串是否以某個指定的字符串結尾 * boolean isempty():判斷字符串是否為空。 * * 注意: * 字符串內容為空和字符串對象為空。 * string s = "";//對象存在,所以可以調方法 * string s = null;//對象不存在,不能調方法 */ public class stringdemo { public static void main(string[] args) { // 創建字符串對象 string s1 = "helloworld" ; string s2 = "helloworld" ; string s3 = "helloworld" ; // boolean equals(object obj):比較字符串的內容是否相同,區分大小寫 system.out.println( "equals:" + s1.equals(s2)); system.out.println( "equals:" + s1.equals(s3)); system.out.println( "-----------------------" ); // boolean equalsignorecase(string str):比較字符串的內容是否相同,忽略大小寫 system.out.println( "equals:" + s1.equalsignorecase(s2)); system.out.println( "equals:" + s1.equalsignorecase(s3)); system.out.println( "-----------------------" ); // boolean contains(string str):判斷大字符串中是否包含小字符串 system.out.println( "contains:" + s1.contains( "hello" )); system.out.println( "contains:" + s1.contains( "hw" )); system.out.println( "-----------------------" ); // boolean startswith(string str):判斷字符串是否以某個指定的字符串開頭 system.out.println( "startswith:" + s1.startswith( "h" )); system.out.println( "startswith:" + s1.startswith( "hello" )); system.out.println( "startswith:" + s1.startswith( "world" )); system.out.println( "-----------------------" ); // 練習:boolean endswith(string str):判斷字符串是否以某個指定的字符串結尾這個自己玩 // boolean isempty():判斷字符串是否為空。 system.out.println( "isempty:" + s1.isempty()); string s4 = "" ; string s5 = null ; system.out.println( "isempty:" + s4.isempty()); // nullpointerexception // s5對象都不存在,所以不能調用方法,空指針異常 // system.out.println("isempty:" + s5.isempty()); } } |
string類的判斷功能---使用---鍵盤錄入猜字小游戲
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
42
43
44
45
46
47
|
package cn.itcast_03; import java.util.scanner; /* * 模擬登錄,給三次機會,并提示還有幾次。如果登錄成功,就可以玩猜數字小游戲了。 * * 分析: * a:定義用戶名和密碼。已存在的。 * b:鍵盤錄入用戶名和密碼。 * c:比較用戶名和密碼。 * 如果都相同,則登錄成功 * 如果有一個不同,則登錄失敗 * d:給三次機會,用循環改進,最好用for循環。 */ public class stringtest2 { public static void main(string[] args) { // 定義用戶名和密碼。已存在的。 string username = "admin" ; string password = "admin" ; // 給三次機會,用循環改進,最好用for循環。 for ( int x = 0 ; x < 3 ; x++) { // x=0,1,2 // 鍵盤錄入用戶名和密碼。 scanner sc = new scanner(system.in); system.out.println( "請輸入用戶名:" ); string name = sc.nextline(); system.out.println( "請輸入密碼:" ); string pwd = sc.nextline(); // 比較用戶名和密碼。 if (name.equals(username) && pwd.equals(password)) { // 如果都相同,則登錄成功 system.out.println( "登錄成功,開始玩游戲" ); //猜數字游戲 guessnumbergame.start(); break ; } else { // 如果有一個不同,則登錄失敗 // 2,1,0 // 如果是第0次,應該換一種提示 if (( 2 - x) == 0 ) { system.out.println( "帳號被鎖定,請與班長聯系" ); } else { system.out.println( "登錄失敗,你還有" + ( 2 - x) + "次機會" ); } } } } } |
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
|
package cn.itcast_03; import java.util.scanner; /* * 這時猜數字小游戲的代碼 */ public class guessnumbergame { private guessnumbergame() { } public static void start() { // 產生一個隨機數 int number = ( int ) (math.random() * 100 ) + 1 ; while ( true ) { // 鍵盤錄入數據 scanner sc = new scanner(system.in); system.out.println( "請輸入你要猜的數據(1-100):" ); int guessnumber = sc.nextint(); // 判斷 if (guessnumber > number) { system.out.println( "你猜的數據" + guessnumber + "大了" ); } else if (guessnumber < number) { system.out.println( "你猜的數據" + guessnumber + "小了" ); } else { system.out.println( "恭喜你,猜中了" ); break ; } } } } |
string類的獲取功能
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
42
43
44
45
46
47
|
package cn.itcast_04; /* * string類的獲取功能 * int length():獲取字符串的長度。 * char charat(int index):獲取指定索引位置的字符 * int indexof(int ch):返回指定字符在此字符串中第一次出現處的索引。 * 為什么這里是int類型,而不是char類型? * 原因是:'a'和97其實都可以代表'a'。如果里面寫char,就不能寫數字97了 * int indexof(string str):返回指定字符串在此字符串中第一次出現處的索引。 * int indexof(int ch,int fromindex):返回指定字符在此字符串中從指定位置后第一次出現處的索引。 * int indexof(string str,int fromindex):返回指定字符串在此字符串中從指定位置后第一次出現處的索引。 * string substring(int start):從指定位置開始截取字符串,默認到末尾。 * string substring(int start,int end):從指定位置開始到指定位置結束截取字符串。 */ public class stringdemo { public static void main(string[] args) { // 定義一個字符串對象 string s = "helloworld" ; // int length():獲取字符串的長度。 system.out.println( "s.length:" + s.length()); //10 system.out.println( "----------------------" ); // char charat(int index):獲取指定索引位置的字符 system.out.println( "charat:" + s.charat( 7 )); // system.out.println( "----------------------" ); // int indexof(int ch):返回指定字符在此字符串中第一次出現處的索引。 system.out.println( "indexof:" + s.indexof( 'l' )); system.out.println( "----------------------" ); // int indexof(string str):返回指定字符串在此字符串中第一次出現處的索引。 system.out.println( "indexof:" + s.indexof( "owo" )); system.out.println( "----------------------" ); // int indexof(int ch,int fromindex):返回指定字符在此字符串中從指定位置后第一次出現處的索引。 system.out.println( "indexof:" + s.indexof( 'l' , 4 )); system.out.println( "indexof:" + s.indexof( 'k' , 4 )); // -1 system.out.println( "indexof:" + s.indexof( 'l' , 40 )); // -1 system.out.println( "----------------------" ); // 自己練習:int indexof(string str,int // fromindex):返回指定字符串在此字符串中從指定位置后第一次出現處的索引。 // string substring(int start):從指定位置開始截取字符串,默認到末尾。包含start這個索引 system.out.println( "substring:" + s.substring( 5 )); system.out.println( "substring:" + s.substring( 0 )); system.out.println( "----------------------" ); // string substring(int start,intend):從指定位置開始到指定位置結束截取字符串。 //包括start索引但是不包end索引 system.out.println( "substring:" + s.substring( 3 , 8 )); system.out.println( "substring:" + s.substring( 0 , s.length())); } } |
字符串遍歷:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package cn.itcast_04; /* * 需求:遍歷獲取字符串中的每一個字符 * * 分析: * a:如何能夠拿到每一個字符呢? * char charat(int index) * b:我怎么知道字符到底有多少個呢? * int length() */ public class stringtest { public static void main(string[] args) { // 定義字符串 string s = "helloworld" ; for ( int x = 0 ; x < s.length(); x++) { system.out.println(s.charat(x)); } } } |
統計大寫字母,小寫字母,數字在字符串中的個數
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package cn.itcast_04; /* * 需求:統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數。(不考慮其他字符) * 舉例: * "hello123world" * 結果: * 大寫字符:2個 * 小寫字符:8個 * 數字字符:3個 * * 分析: * 前提:字符串要存在 * a:定義三個統計變量 * bigcount=0 * smallcount=0 * numbercount=0 * b:遍歷字符串,得到每一個字符。 * length()和charat()結合 * c:判斷該字符到底是屬于那種類型的 * 大:bigcount++ * 小:smallcount++ * 數字:numbercount++ * * 這道題目的難點就是如何判斷某個字符是大的,還是小的,還是數字的。 * ascii碼表: * 0 48 * a 65 * a 97 * 雖然,我們按照數字的這種比較是可以的,但是想多了,有比這還簡單的 * char ch = s.charat(x); * * if(ch>='0' && ch<='9') numbercount++ * if(ch>='a' && ch<='z') smallcount++ * if(ch>='a' && ch<='z') bigcount++ * d:輸出結果。 * * 練習:把給定字符串的方式,改進為鍵盤錄入字符串的方式。 */ public class stringtest2 { public static void main(string[] args) { //定義一個字符串 string s = "hello123world" ; //定義三個統計變量 int bigcount = 0 ; int smallcount = 0 ; int numbercount = 0 ; //遍歷字符串,得到每一個字符。 for ( int x= 0 ; x<s.length(); x++){ char ch = s.charat(x); //判斷該字符到底是屬于那種類型的,char類型會轉成int類型 if (ch>= 'a' && ch<= 'z' ){ smallcount++; } else if (ch>= 'a' && ch<= 'z' ){ bigcount++; } else if (ch>= '0' && ch<= '9' ){ numbercount++; } } //輸出結果。 system.out.println( "大寫字母" +bigcount+ "個" ); system.out.println( "小寫字母" +smallcount+ "個" ); system.out.println( "數字" +numbercount+ "個" ); } } |
string的轉換功能:
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
42
43
44
45
46
47
48
49
50
51
52
53
|
package cn.itcast_05; /* * string的轉換功能: * byte[] getbytes():把字符串轉換為字節數組。 * char[] tochararray():把字符串轉換為字符數組。 * static string valueof(char[] chs):把字符數組轉成字符串。 * static string valueof(int i):把int類型的數據轉成字符串。 * 注意:string類的valueof方法可以把任意類型的數據轉成字符串。 * string tolowercase():把字符串轉成小寫。 * string touppercase():把字符串轉成大寫。 * string concat(string str):把字符串拼接。 */ public class stringdemo { public static void main(string[] args) { // 定義一個字符串對象 string s = "javase" ; // byte[] getbytes():把字符串轉換為字節數組。 byte [] bys = s.getbytes(); for ( int x = 0 ; x < bys.length; x++) { system.out.println(bys[x]); } system.out.println( "----------------" ); // char[] tochararray():把字符串轉換為字符數組。 char [] chs = s.tochararray(); for ( int x = 0 ; x < chs.length; x++) { system.out.println(chs[x]); } system.out.println( "----------------" ); // static string valueof(char[] chs):把字符數組轉成字符串。 string ss = string.valueof(chs); system.out.println(ss); system.out.println( "----------------" ); // static string valueof(int i):把int類型的數據轉成字符串。 int i = 100 ; string sss = string.valueof(i); system.out.println(sss); system.out.println( "----------------" ); // string tolowercase():把字符串轉成小寫。 system.out.println( "tolowercase:" + s.tolowercase()); system.out.println( "s:" + s); // system.out.println("----------------"); // string touppercase():把字符串轉成大寫。 system.out.println( "touppercase:" + s.touppercase()); system.out.println( "----------------" ); // string concat(string str):把字符串拼接。 string s1 = "hello" ; string s2 = "world" ; string s3 = s1 + s2; string s4 = s1.concat(s2); system.out.println( "s3:" +s3); system.out.println( "s4:" +s4); } } |
把一個字符串的首字母轉成大寫,其余為小寫。(只考慮英文大小寫字母字符)
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
|
package cn.itcast_05; /* * 需求:把一個字符串的首字母轉成大寫,其余為小寫。(只考慮英文大小寫字母字符) * 舉例: * helloworld * 結果: * helloworld * * 分析: * a:先獲取第一個字符 * b:獲取除了第一個字符以外的字符 * c:把a轉成大寫 * d:把b轉成小寫 * e:c拼接d */ public class stringtest { public static void main(string[] args) { // 定義一個字符串 string s = "helloworld" ; // 先獲取第一個字符 string s1 = s.substring( 0 , 1 ); // 獲取除了第一個字符以外的字符 string s2 = s.substring( 1 ); // 把a轉成大寫 string s3 = s1.touppercase(); // 把b轉成小寫 string s4 = s2.tolowercase(); // c拼接d string s5 = s3.concat(s4); system.out.println(s5); // 優化后的代碼 // 鏈式編程 string result = s.substring( 0 , 1 ).touppercase() .concat(s.substring( 1 ).tolowercase()); system.out.println(result); } } |
string類的其他功能:
- 替換功能:
- 去除字符串兩空格
- 按字典順序比較兩個字符串
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
|
package cn.itcast_06; /* * string類的其他功能: * * 替換功能: * string replace(char old,char new) * string replace(string old,string new) * * 去除字符串兩空格 * string trim() * * 按字典順序比較兩個字符串 * int compareto(string str) * int comparetoignorecase(string str) */ public class stringdemo { public static void main(string[] args) { // 替換功能 string s1 = "helloworld" ; string s2 = s1.replace( 'l' , 'k' ); string s3 = s1.replace( "owo" , "ak47" ); system.out.println( "s1:" + s1); system.out.println( "s2:" + s2); system.out.println( "s3:" + s3); system.out.println( "---------------" ); // 去除字符串兩空格 string s4 = " hello world " ; string s5 = s4.trim(); system.out.println( "s4:" + s4 + "---" ); system.out.println( "s5:" + s5 + "---" ); // 按字典順序比較兩個字符串 string s6 = "hello" ; string s7 = "hello" ; string s8 = "abc" ; string s9 = "xyz" ; system.out.println(s6.compareto(s7)); // 0 system.out.println(s6.compareto(s8)); // 7 system.out.println(s6.compareto(s9)); // -16 } } |
compareto源碼解析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public int compareto(string anotherstring) { //this -- s1 -- "hello" //anotherstring -- s2 -- "hel" int len1 = value.length; //this.value.length--s1.tochararray().length--5 int len2 = anotherstring.value.length; //s2.value.length -- s2.tochararray().length--3 int lim = math.min(len1, len2); //math.min(5,3); -- lim=3; char v1[] = value; //s1.tochararray() char v2[] = anotherstring.value; //char v1[] = {'h','e','l','l','o'}; //char v2[] = {'h','e','l'}; int k = 0 ; while (k < lim) { char c1 = v1[k]; //c1='h','e','l' char c2 = v2[k]; //c2='h','e','l' if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; //5-3=2; } string s1 = "hello" ; string s2 = "hel" ; system.out.println(s1.compareto(s2)); // 2 |
把數組中的數據按照指定個格式拼接成一個字符串
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
42
43
44
45
46
47
48
49
50
|
package cn.itcast_07; /* * 需求:把數組中的數據按照指定個格式拼接成一個字符串 * 舉例: * int[] arr = {1,2,3}; * 輸出結果: * "[1, 2, 3]" * 分析: * a:定義一個字符串對象,只不過內容為空 * b:先把字符串拼接一個"[" * c:遍歷int數組,得到每一個元素 * d:先判斷該元素是否為最后一個 * 是:就直接拼接元素和"]" * 不是:就拼接元素和逗號以及空格 * e:輸出拼接后的字符串 * * 把代碼用功能實現。 */ public class stringtest2 { public static void main(string[] args) { // 前提是數組已經存在 int[] arr = { 1, 2, 3 }; // 寫一個功能,實現結果 string result = arraytostring(arr); system.out.println("最終結果是:" + result); } /* * 兩個明確: 返回值類型:string 參數列表:int[] arr */ public static string arraytostring( int [] arr) { // 定義一個字符串 string s = "" ; // 先把字符串拼接一個"[" s += "[" ; // 遍歷int數組,得到每一個元素 for ( int x = 0 ; x < arr.length; x++) { // 先判斷該元素是否為最后一個 if (x == arr.length - 1 ) { // 就直接拼接元素和"]" s += arr[x]; s += "]" ; } else { // 就拼接元素和逗號以及空格 s += arr[x]; s += ", " ; } } return s; } } |
字符串反轉
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
|
package cn.itcast_07; import java.util.scanner; /* * 字符串反轉 * 舉例:鍵盤錄入"abc" * 輸出結果:"cba" * * 分析: * a:鍵盤錄入一個字符串 * b:定義一個新字符串 * c:倒著遍歷字符串,得到每一個字符 * a:length()和charat()結合 * b:把字符串轉成字符數組 * d:用新字符串把每一個字符拼接起來 * e:輸出新串 */ public class stringtest3 { public static void main(string[] args) { // 鍵盤錄入一個字符串 scanner sc = new scanner(system.in); system.out.println("請輸入一個字符串:"); string line = sc.nextline(); string s = myreverse(line); system.out.println("實現功能后的結果是:" + s); } /* * 兩個明確: 返回值類型:string 參數列表:string */ public static string myreverse(string s) { // 定義一個新字符串 string result = "" ; // 把字符串轉成字符數組 char [] chs = s.tochararray(); // 倒著遍歷字符串,得到每一個字符 for ( int x = chs.length - 1 ; x >= 0 ; x--) { // 用新字符串把每一個字符拼接起來 result += chs[x]; } return result; } } |
統計大串中小串出現的次數
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
42
43
|
package cn.itcast_07; /* * 統計大串中小串出現的次數 * 舉例: * 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun" * 結果: * java出現了5次 * * 分析: * 前提:是已經知道了大串和小串。 * * a:定義一個統計變量,初始化值是0 * b:先在大串中查找一次小串第一次出現的位置 * a:索引是-1,說明不存在了,就返回統計變量 * b:索引不是-1,說明存在,統計變量++ * c:把剛才的索引+小串的長度作為開始位置截取上一次的大串,返回一個新的字符串,并把該字符串的值重新賦值給大串 * d:回到b */ public class stringtest5 { public static void main(string[] args) { // 定義大串 string maxstring = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"; // 定義小串 string minstring = "java"; // 寫功能實現 int count = getcount(maxstring, minstring); system.out.println("java在大串中出現了:" + count + "次"); } /* * 兩個明確: 返回值類型:int 參數列表:兩個字符串 */ public static int getcount(string maxstring, string minstring) { // 定義一個統計變量,初始化值是0 int count = 0 ; int index; //先查,賦值,判斷 while ((index=maxstring.indexof(minstring))!=- 1 ){ count++; maxstring = maxstring.substring(index + minstring.length()); } return count; } } |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://www.cnblogs.com/baiyangyuanzi/p/6855703.html