当前位置:网站首页>在一个字符串里面统计给定字符串的个数
在一个字符串里面统计给定字符串的个数
2022-07-27 23:17:00 【SSS4362】
在一个字符串里面统计给定字符串的个数
1.拆分法
1.1 思路分析
例如字符串为"1java2java3e"以java为拆分字符串的话
得到的字符串数组 String[] strs={“1”,“2”,“3e”},其长度为3
而满足条件的字符串个数=2=字符串数组长度-1,因而就有如下结论:
拆分得到的字符串长度-1=给定字符的个数
1.2 示例源码
package Work;
public class Test05 {
public static void main(String[] args) {
System.out.println(countString("1java23javajav", "java"));
System.out.println(countString("zdhwefyg", "java"));
}
public static int countString(String str,String regex){
//str为原字符串,regex为给定字符串
return str.split(regex).length-1;
}
}
1.3 示例源码运行截图

2.替换统计法
2.1 思路分析
设原字符串为str1
将原字符串中存在的给定字符串替换成""(空串),得到一个新字符串str2;
给定字符串的数量=(str1-str2)/给定字符串的长度
2.2 示例源码
package Work;
public class Test05 {
public static void main(String[] args) {
System.out.println(countString("java1java23javajav", "java"));
System.out.println(countString("zdhwefyg", "java"));
}
public static int countString(String str,String regex){
//str为原字符串,regex为给定字符串
return (str.length()-str.replace(regex,"").length())/regex.length();
}
}
2.3 示例源码运行截图

3.下标法
3.1 思路分析
通过indexOf找到出现给定字符串的第一次出现位置的下标index,
若为-1,统计结果为0
若不为-1,就进入循环(循环终止条件为indexOf得到的结果为-1).此时count数量需要加1
再从找到的字符串中最后一个字符的后一位(index+regex.length)开始寻找是否有给定的字符串,若有,则不退出循环,count继续加1,然后继续往后找(找的位置依然和前面是同一个规律),直到后面没有给定字符串了,那么就结束循环,最终返回的就是count的数量
3.2 示例源码
package Work;
public class Test05 {
public static void main(String[] args) {
System.out.println(countString("java1java23javajav", "java"));
System.out.println(countString("zdhwefyg", "java"));
}
public static int countString(String str,String regex){
//str为原字符串,regex为给定字符串
int index=str.indexOf(regex);
//第一次找原来字符串有没有给定字符串,如果为-1(代表没有),那就没有必要往后去找了,如果有就进入循环去找
int count=0;
while (index!=-1){
//进入循环就count+1
count++;
//然后每次都需要更新index的值,因为要从找到字符串的最后一个位置的后一位开始找
//没有必要再去从前往后找了
index=str.indexOf(regex,index+regex.length());
}
return count;
}
}
3.3 示例源码运行截图

4.下标+截取子串法
4.1 分析
通过indexOf找到出现给定字符串的第一次出现位置的下标index,
如果为-1,统计结果就为0,没有必要再去往后截取了
如果不为-1,就进入循环里面去找(循环成立的条件就是不为-1),
循环每成立一次count+1,然后成立后你需要从找到的给定字符串的最后一位字符的后一位开始往后截取(因为前面已经比较完了,没有截取的必要了)
截取完后需要获取截取后的子串中的给定字符串的下标,若不为-1就继续循环,否则
退出循环,然后一直这样截取然后加上获得下标下去,直到得到的下标为-1,就退出循环
4.2 示例源码
package Work;
public class Test05 {
public static void main(String[] args) {
System.out.println(countString("j2ava1java23javajav", "java"));
System.out.println(countString("zdhwefyg", "java"));
}
public static int countString(String str,String regex){
//str为原字符串,regex为给定字符串
int index=str.indexOf(regex);
//第一次找原来字符串有没有给定字符串,如果为-1(代表没有),那就没有必要往后去找了,如果有就进入循环去找
int count=0;
while (index!=-1){
//进入循环就count+1
count++;
//每弄一次都需要截取+获取截取后子串中给定字符串的位置
str=str.substring(index+regex.length());
//indexOf默认还是从0开始,只不过是从字符串的第一位开始而已
index=str.indexOf(regex);
}
return count;
}
}
4.3 示例源码运行截图

边栏推荐
猜你喜欢
随机推荐
Swoole Task任务使用
Anfulai embedded weekly report no. 275: 2022.07.18--2022.07.24
Gazebo 控制实例
Data problems can also be found if there is a space at the end of the field value of MySQL query criteria
110. In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe
C language programming | explanation and Simulation of offsetof macro
How the test architects of bat factories interpret various disputes of the test platform
Insider of container network hard core technology (7) sailing on the sea depends on the helmsman
Tear the source code of gateway by hand, and tear the source code of workflow and load balancing today
Redis-哨兵模式
Flutter 通话界面UI
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Swoole定时器
Shenzhen Huaqiang announced that it plans to invest no more than 20million yuan in BYD semiconductor
【游戏】任天堂Nintendo Switch超详细购买/使用指南以及注意事项(根据自己使用持续更新中...)
Meguiar sued liandian for secret theft and sentenced: liandian was fined NT $100million and three employees were sentenced!
Brief analysis of advantages, disadvantages and development of SAP modules
MySQL JPA support for JSON type data in database
C语言main函数传递参数
Matlab drawing - points and vectors: method and source code of vector addition and subtraction









