【實用類String】String類方法的應用案例:查找判斷指定字符出現的次數和位置


一、應用要求

  輸入一個字符串,再輸入要查找的字符,判斷該字符在該字符串中出現的次數。

二、實現思路

  1.使用substring()方法將字符串的每個字符存入數組

  2.比較數組每個字符是否與指定的字符相等,並計數

三、編寫代碼

  錯誤案例:

 1 import java.util.Scanner;
 2 
 3 public class StringDemo {
 4 
 5     public static void main(String[] args) {
 6         Scanner input = new Scanner(System.in);
 7         System.out.println("請輸入一句話:");
 8         String string = input.next();
 9         System.out.println("請輸入查找的字符串:");
10         String s = input.next();
11         
12         
13         //2.正序
14         /*int num = 0;
15         int temp = 0;
16         while(string.indexOf(s)>-1){
17             int index = string.indexOf(s);
18             num=num+index+temp;
19             System.out.print(num+"\t");
20             string = string.substring(index+1);
21             temp++;
22         }*/
23         /*while(string.indexOf(s)>-1){
24             if (temp == 0) {
25                 System.out.println(string.indexOf(s));
26                 num = string.indexOf(s);
27                 string = string.substring(string.indexOf(s)+1);
28                 temp++;
29             } else {
30                 int indexs = string.indexOf(s);
31                 string = string.substring(indexs+1);
32                 num=num+indexs+1;
33                 System.out.print(num+"\t");
34             }
35             
36         }*/
37         /*從后往前找,遇到重復字符會出錯
38          * 
39         //1.查找第一個下標
40         int index = string.indexOf(s);
41         //聲明一個集合保存獲取的下標
42         List<Integer> list = new ArrayList<Integer>();
43         while(string.length()>=index){
44             //2.查找最后一個
45             int indexEnd = string.lastIndexOf(s);
46             if (indexEnd<0) {
47                 break;
48             }
49             list.add(indexEnd);//保存到list
50             //System.out.print(indexEnd+"\t");
51             string = string.substring(0, indexEnd);
52         }
53         //遍歷list
54         System.out.println();
55         for (int i = list.size()-1; i >= 0; i--) {
56             System.out.print(list.get(i)+"\t");
57         }
58         */
59         //查找字符打印位置下標:正序查找才是王道
60         for (int i = 0; i <= string.lastIndexOf(s); i++) {
61             if (s.equals(string.substring(i, i+s.length()))) {
62                 System.out.println(i);
63                 i = i+s.length()-1;
64             }
65         }
66         input.close();
67 
68     }
69 
70 }

  最終代碼:

 1 import java.util.Scanner;
 2 /**
 3  * 查找字符串
 4  * @author Administrator
 5  *
 6  */
 7 public class String01 {
 8 
 9     public static void main(String[] args) {
10         Scanner input = new Scanner(System.in);
11         System.out.println("請輸入一句話:");
12         String string = input.next();
13         System.out.print("請輸入查找的字符串:");
14         String s = input.next();
15         //查找字符打印位置下標
16         int count = 0;//記錄字符出現的次數
17         for (int i = 0; i <= string.lastIndexOf(s); i++) {
18             if (s.equals(string.substring(i, i+s.length()))) {
19                 System.out.print(i+"\t");
20                 i = i+s.length()-1;
21                 count++;
22             }
23         }
24         System.out.println(s+"共出現"+count+"次!");
25         input.close();
26 
27     }
28 
29 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM