一、应用要求

  输入一个字符串,再输入要查找的字符,判断该字符在该字符串中出现的次数。

二、实现思路

  1.使用substring()方法将字符串的每个字符存入数组

  2.比较数组每个字符是否与指定的字符相等,并计数

三、编写代码

  错误案例:

 import java.util.Scanner;

 public class StringDemo {

     public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一句话:");
String string = input.next();
System.out.println("请输入查找的字符串:");
String s = input.next(); //2.正序
/*int num = 0;
int temp = 0;
while(string.indexOf(s)>-1){
int index = string.indexOf(s);
num=num+index+temp;
System.out.print(num+"\t");
string = string.substring(index+1);
temp++;
}*/
/*while(string.indexOf(s)>-1){
if (temp == 0) {
System.out.println(string.indexOf(s));
num = string.indexOf(s);
string = string.substring(string.indexOf(s)+1);
temp++;
} else {
int indexs = string.indexOf(s);
string = string.substring(indexs+1);
num=num+indexs+1;
System.out.print(num+"\t");
} }*/
/*从后往前找,遇到重复字符会出错
*
//1.查找第一个下标
int index = string.indexOf(s);
//声明一个集合保存获取的下标
List<Integer> list = new ArrayList<Integer>();
while(string.length()>=index){
//2.查找最后一个
int indexEnd = string.lastIndexOf(s);
if (indexEnd<0) {
break;
}
list.add(indexEnd);//保存到list
//System.out.print(indexEnd+"\t");
string = string.substring(0, indexEnd);
}
//遍历list
System.out.println();
for (int i = list.size()-1; i >= 0; i--) {
System.out.print(list.get(i)+"\t");
}
*/
//查找字符打印位置下标:正序查找才是王道
for (int i = 0; i <= string.lastIndexOf(s); i++) {
if (s.equals(string.substring(i, i+s.length()))) {
System.out.println(i);
i = i+s.length()-1;
}
}
input.close(); } }

  最终代码:

 import java.util.Scanner;
/**
* 查找字符串
* @author Administrator
*
*/
public class String01 { public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一句话:");
String string = input.next();
System.out.print("请输入查找的字符串:");
String s = input.next();
//查找字符打印位置下标
int count = 0;//记录字符出现的次数
for (int i = 0; i <= string.lastIndexOf(s); i++) {
if (s.equals(string.substring(i, i+s.length()))) {
System.out.print(i+"\t");
i = i+s.length()-1;
count++;
}
}
System.out.println(s+"共出现"+count+"次!");
input.close(); } }

【实用类String】String类方法的应用案例:查找判断指定字符出现的次数和位置的更多相关文章

  1. java 基础 - 查找某个字串出现的次数及位置

    查找某个字串出现的次数及位置 public class search { public static void main(String[] args){ String str = "abc1 ...

  2. SQL Server 查找字符串中指定字符出现的次数

    要查找某个指定的字符在字符串中出现的位置,方法比较简单,使用 len() 函数和 replace() 函数结合就可以. SELECT TOP 200 approveInfo approveInfo2, ...

  3. Day_09【常用API】扩展案例3_删除源字符串中的指定字符,并计算指定字符出现的次数

    分析以下需求,并用代码实现 1.键盘录入一个源字符串由字符串变量scrStr接收 2.键盘录入一个要删除的字符串由字符串变量delStr接收 3.要求 删除该字scrStr符串中的所有delStr字符 ...

  4. 使用grep查找文件中指定字符出现的次数

    grep -o ‘好' 文件名.txt | wc -l -o 指示grep显示所有匹配的地方,并且每一个匹配单独一行输出.这样只要统计输出的行数就可以知道这个字符出现的次数了.

  5. Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例

    1. HashMap集合(HashMap<String,Student>)的案例 HashMap是最常用的Map集合,它的键值对在存储时要根据键的哈希码来确定值放在哪里. HashMap的 ...

  6. 用 string 进行插入、替代、查找输出下标等操作

    string s; s = "; string::iterator it; it = s.begin();//让s指向第一个元素 cout << s; system(" ...

  7. C#基础总结之五Dictionary<string, string[]>和while循环

    #region 第五天作业 名片集(01) //Dictionary<string, string[]> PersonCard = new Dictionary<string, st ...

  8. Babelfish (关于map<string,string>的用法

    题目链接:https://vjudge.net/contest/237395#problem/A 学习博客:https://blog.csdn.net/lyy289065406/article/det ...

  9. String类(获取,转换,判断,比较)

    1 package cn.itcast.p1.string.demo; 2 3 import java.util.Iterator; 4 5 import com.sun.org.apache.xpa ...

随机推荐

  1. 【转】右键的 在 vs 中打开 怎么去掉

    源地址:https://blog.csdn.net/weicaijiang/article/details/78818522 HKEY_CLASSES_ROOT\Directory\backgroun ...

  2. BZOJ 1061 [Noi2008]志愿者招募(费用流)

    题目描述 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完成,其中第i ...

  3. javascript中构造器(函数)的__proto__与prototype初探

    背景:最近没什么需求,快要闲出屁了,所以重温了一下js的原型,结果大有收获,且偶然看到Snandy大神的<JavaScript中__proto__与prototype的关系> 这篇文章,感 ...

  4. 关于nmake cl.exe error 0xc0000135

    [问题] F:\STLport-5.1.4\build\lib>nmake /f msvc.mak cl /nologo /W4 /Wp64 /GR /EHsc /Zm800 /GL /MD / ...

  5. 跟我一起读postgresql源码(三)——Rewrite(查询重写模块)

    上一篇博文我们阅读了postgresql中查询分析模块的源码.查询分析模块对前台送来的命令进行词法分析.语法分析和语义分析后获得对应的查询树(Query).在获得查询树之后,程序开始对查询树进行查询重 ...

  6. 4A - Horse

    打表找规律 #include <iostream> #include <queue> using namespace std; ][]; ]{, , , , -, -, -, ...

  7. flask_restful

    from flask_restful import (Resource, reqparse) # 参数解析对象生成 parser = reqparse.RequestParser() parser.a ...

  8. ui-grid 中cellTemplate中click事件

    cellTemplate中使用的函数: 外部定义的函数:

  9. POJ - 3696 同余

    给定\(L\),求最小的\(x\)满足$ L|8\frac{10^x-1}{9} $ /*H E A D*/ inline ll gcd(ll a,ll b){return b?gcd(b,a%b): ...

  10. Python入门(1)

    1.编程语言 机器语言:直接用计算机能听懂的二进制指令去编写程序,需要了解硬件的细节 汇编语言:用英文标签取代二进制指令去编写程序,同样需要了解硬件的细节 高级语言:直接用人类能理解的表达方式去编写程 ...