【实用类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 }
posted @ 2017-05-28 18:22  滕秋宇  阅读(4175)  评论(0编辑  收藏  举报