当前位置:网站首页>Count the number of given strings in a string
Count the number of given strings in a string
2022-07-28 01:26:00 【SSS4362】
Count the number of given strings in a string
1. Split method
1.1 Thought analysis
For example, the string is "1java2java3e" With java To split strings
The resulting string array String[] strs={“1”,“2”,“3e”}, The length of 3
And the number of strings that meet the conditions =2= String array length -1, Therefore, we have the following conclusions :
The length of the split string -1= Number of given characters
1.2 Sample source code
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 Is the original string ,regex For the given string
return str.split(regex).length-1;
}
}
1.3 Screenshot of sample source code

2. Replace the statistical method
2.1 Thought analysis
Set the original string to str1
Replace the given string existing in the original string with ""( Empty string ), Get a new string str2;
The number of given strings =(str1-str2)/ The length of a given string
2.2 Sample source code
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 Is the original string ,regex For the given string
return (str.length()-str.replace(regex,"").length())/regex.length();
}
}
2.3 Screenshot of sample source code

3. The subscript method
3.1 Thought analysis
adopt indexOf Find the subscript where the given string first appears index,
if -1, The statistical result is 0
If not -1, Go into the cycle ( The loop termination condition is indexOf The result is zero -1). here count The quantity needs to be increased 1
Then from the last character in the found string (index+regex.length) Start looking for a given string , If you have any , Do not exit the loop ,count Keep adding 1, Then continue to look back ( Looking for the position is still the same rule as the front ), Until there is no given string , So end the cycle , The final return is count The number of
3.2 Sample source code
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 Is the original string ,regex For the given string
int index=str.indexOf(regex);
// Find whether the original string has a given string for the first time , If -1( It means there is no ), Then there is no need to look back , If there is, enter the cycle to find
int count=0;
while (index!=-1){
// Enter the cycle count+1
count++;
// Then it needs to be updated every time index Value , Because you need to start from the last position of the string
// There is no need to look from the past to the future
index=str.indexOf(regex,index+regex.length());
}
return count;
}
}
3.3 Screenshot of sample source code

4. Subscript + Intercepting substring method
4.1 analysis
adopt indexOf Find the subscript where the given string first appears index,
If -1, The statistical result is 0, There is no need to intercept later
If not for -1, Just go into the cycle to find ( The condition of circulation is not -1),
Every time the cycle is established count+1, Then after it is established, you need to intercept from the last character of the given string ( Because the previous comparison is over , There is no need to intercept )
After interception You need to get the subscript of the given string in the intercepted substring , If not -1 Just keep cycling , otherwise
Exit loop , Then intercept like this all the time, and then add the subscript to get it , Until you get the subscript -1, Just exit the loop
4.2 Sample source code
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 Is the original string ,regex For the given string
int index=str.indexOf(regex);
// Find whether the original string has a given string for the first time , If -1( It means there is no ), Then there is no need to look back , If there is, enter the cycle to find
int count=0;
while (index!=-1){
// Enter the cycle count+1
count++;
// Every time you do it, you need to intercept + Get the position of the given string in the substring after interception
str=str.substring(index+regex.length());
//indexOf The default is from 0 Start , It just starts from the first bit of the string
index=str.indexOf(regex);
}
return count;
}
}
4.3 Screenshot of sample source code

边栏推荐
- Adding custom dynamic arts and Sciences to cesium
- How to make digital retail undertake the development task of the era of traffic and retention may be the key
- mysql-JPA对数据库中JSON类型数据的支持
- Operator depth anatomy
- From functional testing to automated testing, my monthly salary has exceeded 30k+, and I have 6 years of testing experience.
- Codeforces暑期训练周报(7.21~7.27)
- Briefly understand namenode and datanode
- If asynchronous processing is implemented according to the framework
- Anfulai embedded weekly report no. 275: 2022.07.18--2022.07.24
- The cooperation between starfish OS and metabell is just the beginning
猜你喜欢

BSP video tutorial issue 21: easy one key implementation of serial port DMA variable length transceiver, support bare metal and RTOS, including MDK and IAR, which is more convenient than stm32cubemx (

Basic concept and classification of i/o equipment

EWM收货ECC交货单校验逻辑问题

Codeforces暑期训练周报(7.21~7.27)

Data problems can also be found if there is a space at the end of the field value of MySQL query criteria

Detailed explanation of retinanet network structure

File system mount

Lecture 16 of project practice: using the open close principle to realize the commodity price rule engine

Fabric2.4.4 version building process (complete process)

二维数组相关知识
随机推荐
Go language variable
Gazebo 控制实例
Cross domain requests in nodejs
The total investment is nearly 1.6 billion yuan! Qianzhao optoelectronics VCSEL and high-end LED chip projects officially started
System clock failure of database fault tolerance
总投资近16亿元!乾照光电VCSEL、高端LED芯片项目正式开工
Flutter--密码登录注册界面
Detailed explanation of retinanet network structure
How the test architects of bat factories interpret various disputes of the test platform
Spool timer
Codeforces暑期训练周报(7.14~7.20)
MySQL JPA support for JSON type data in database
Shenzhen Huaqiang announced that it plans to invest no more than 20million yuan in BYD semiconductor
氧气温湿度模组
Redis-哨兵模式
Swoole定时器
Redis缓存穿透击穿和雪崩
2022/07/27 learning notes (Day17) code blocks and internal classes
Shutter -- password login registration interface
Go 语言变量