【实用类String】String类方法的应用案例:查找判断指定字符出现的次数和位置
一、应用要求
输入一个字符串,再输入要查找的字符,判断该字符在该字符串中出现的次数。
二、实现思路
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类方法的应用案例:查找判断指定字符出现的次数和位置的更多相关文章
- java 基础 - 查找某个字串出现的次数及位置
查找某个字串出现的次数及位置 public class search { public static void main(String[] args){ String str = "abc1 ...
- SQL Server 查找字符串中指定字符出现的次数
要查找某个指定的字符在字符串中出现的位置,方法比较简单,使用 len() 函数和 replace() 函数结合就可以. SELECT TOP 200 approveInfo approveInfo2, ...
- Day_09【常用API】扩展案例3_删除源字符串中的指定字符,并计算指定字符出现的次数
分析以下需求,并用代码实现 1.键盘录入一个源字符串由字符串变量scrStr接收 2.键盘录入一个要删除的字符串由字符串变量delStr接收 3.要求 删除该字scrStr符串中的所有delStr字符 ...
- 使用grep查找文件中指定字符出现的次数
grep -o ‘好' 文件名.txt | wc -l -o 指示grep显示所有匹配的地方,并且每一个匹配单独一行输出.这样只要统计输出的行数就可以知道这个字符出现的次数了.
- Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例
1. HashMap集合(HashMap<String,Student>)的案例 HashMap是最常用的Map集合,它的键值对在存储时要根据键的哈希码来确定值放在哪里. HashMap的 ...
- 用 string 进行插入、替代、查找输出下标等操作
string s; s = "; string::iterator it; it = s.begin();//让s指向第一个元素 cout << s; system(" ...
- C#基础总结之五Dictionary<string, string[]>和while循环
#region 第五天作业 名片集(01) //Dictionary<string, string[]> PersonCard = new Dictionary<string, st ...
- Babelfish (关于map<string,string>的用法
题目链接:https://vjudge.net/contest/237395#problem/A 学习博客:https://blog.csdn.net/lyy289065406/article/det ...
- String类(获取,转换,判断,比较)
1 package cn.itcast.p1.string.demo; 2 3 import java.util.Iterator; 4 5 import com.sun.org.apache.xpa ...
随机推荐
- Kubernetes 集群部署(2) -- Etcd 集群
Kubenetes 集群部署规划: 192.168.137.81 Master 192.168.137.82 Node 192.168.137.83 Node 以下在 Master 节点操作. ...
- 【ARC075F】Mirrored 搜索/数位dp
Description 给定正整数DD,求有多少个正整数NN,满足rev(N)=N+Drev(N)=N+D,其中rev(N)rev(N)表示将NN的十进制表示翻转来读得到的数 Input 一个 ...
- 未来it行业发展方向
https://www.zhihu.com/question/24222456 IT行业,未来10年和20年,技术发展方向会是什么? 本人CS 本科刚毕业,正在选择工作方向.希望之后专注一个方向发展. ...
- postgress数据库 出现大写字母 字段名但是提示说不存在
select BSK001 from dbdata 报错: column "bsk001" of relation "dbdata" does not exis ...
- Preferences偏好设置
Preferences偏好设置-Preferences偏好设置 General(综合设置):在综合设置界面内,对Unity集成开发环境进行一些相关的设置 Auto Refresh:自动更新 Alway ...
- IO模型之三Reactor 和 Proactor IO设计模式
反应器Reactor: 在事件驱动的应用中,应用中的请求总是通过事件(如CONNECTOR.READ.WRITE等)来表示,当多个请求同时到来时,这些请求最终还是会被序列化地处理,在序列化处理这些服务 ...
- windows cmd下创建虚拟环境virtualenv
一:虚拟环境virtualenv 如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就无 ...
- window平台 php 安装 redis 扩展
1.使用phpinfo() 函数查看PHP的版本信息 <?php phpinfo(); ?> 查看扩展文件版本(特别注意以php版本的 architecture 是x86还是64为准,不能 ...
- 天梯赛easy题
2 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> ...
- Oracle分析函数系列之first_value/last_value:在记录集中查找第一条记录和最后一条记录
[转自] http://blog.csdn.net/rfb0204421/article/details/7675911 注意:与max,min的区别,虽然也可以实现,但只是针对数字字段. 1.初始化 ...