【实用类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 ...
随机推荐
- HDP 中 yarn 和 MR2 的配置
以下说明均以集群中 slave 结点的配置为 48G内存,12块硬盘,12核(core) CPU 为例. 在 Yarn 中,一个 Container 是一个基础的包含内存和CPU 的单元.为了较好的平 ...
- 在CentOS-6.3环境下,利用grub工具手工制作Linux U盘安装盘
注:此文是本人亲自操作实现后写的心得,同时也是对自己操作的记录. 制作的全过程概况 准备工作: U盘重新分区: 格式化U盘: 安装grub文件到U盘特定的分区: 拷贝镜像中的相关文件到U盘: 安装时设 ...
- hexo的jacman主题配置
获得更多资料欢迎进入我的网站或者 csdn或者博客园 这是在我搭建博客时用的主题,这个主题时基于pacman修改的,同时我也是借助于wuchong同时他还在一直更新.一下时我的一些基本配置: 相关文章 ...
- .net core UseHttpsRedirection() 正式环境无效
莫名其妙遇到这样的问题.这样的配置在本地可以,正式环境就不行了. ··· public void Configure(IApplicationBuilder app, Microsoft.AspNet ...
- linux下蓝牙开发(bluez应用)
编译blueZ-5.25 需要先编译安装以下包: bluez-libs-3.36.tar.gz expat-2.1.0.tar.gz dbus-1.10.0.tar.gz glib-2.26.1.ta ...
- ThinkPHP5.0中Request请求对象的使用和常用的操作
request的使用 第一种方法 在控制器头部添加request引用 然后在方法里调用 ‘instance’类 然后在调用方法: public function index($name='name') ...
- python 3中 的subprocess
commands好像python3.6被去除了,它现在被subprocess替代了 FUNCTIONS getoutput(cmd) Return output (stdout or stderr) ...
- 关于Manjaro与Ubuntu双系统并存引发的一个boot问题
事情发生在写下这篇博客的半小时前.笔者的电脑本身是Manjaro+win10双系统并存,因为一些原因要安装ubuntu. 装完ubuntu用了一阵子,想切回manjaro,于是遇到了这个问题. 看到k ...
- C++_IO与文件4-简单文件的输入与输出
通过键盘输入和屏幕输出被称为是控制台输入/输出: 更广义上讲控制台的输入/输出也是一种特殊的文件输入/输出: 当使用cin进行输入时,程序将输入视为一系列的字节,其中的每个字节都被解释成字符编码: 不 ...
- gym101964G Matrix Queries seerc2018k题 cdq分治
题目传送门 题目大意: 二维平面上有q次操作,每次操作可以是添加一个点,也可以是添加一个矩形,问每次操作后,有多少 点-矩形 这样的pair,pair的条件是点被矩形覆盖(边缘覆盖也算). 思路: ...