当前位置:网站首页>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

边栏推荐
- MySQL JPA support for JSON type data in database
- Go 语言变量
- 华为回应美国封锁供应链:他们仍需为5G专利付费
- 闻泰科技收购安世半导体剩余股权获得无条件通过
- Unknown database ‘xxxxx‘
- Redis cache penetration breakdown and avalanche
- URDF 集成 Gazebo
- From functional testing to automated testing, my monthly salary has exceeded 30k+, and I have 6 years of testing experience.
- 字节月薪28K,分享一波我的自动化测试经验....
- Oxygen temperature and humidity module
猜你喜欢

Basic concept and classification of i/o equipment

《安富莱嵌入式周报》第275期:2022.07.18--2022.07.24

ABAP CDS Table Function介绍与示例

Knowledge of two-dimensional array

Unknown database ‘xxxxx‘

Spool timer

从功能测试到自动化测试,月薪突破30K+,我的6年测开经验。

Un7.13: how to add, delete, modify and query in vs Code?

Introduction to the browser video frame operation method requestvideoframecallback()

Unity Shader入门精要学习——基础纹理
随机推荐
6月19日上会,中芯国际或创造国内最快上市记录!
Rviz uses arbotix to control robot motion
Basic concept and classification of i/o equipment
Node red interacts with tdengine
Detailed explanation of retinanet network structure
Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used
If asynchronous processing is implemented according to the framework
I want to get 20K after 3 years of experience, but I haven't got it for half a month?
晶方科技:光刻机厂商ASML为公司参与并购的Anteryon公司的最主要客户之一
美光起诉联电窃密案宣判:联电被罚1亿元新台币,三名员工被判刑!
Briefly understand namenode and datanode
Huami technology "Huangshan No.2" release: AI performance is improved by 7 times, and power consumption is reduced by 50%!
Cesium add dynamic pop-up
Unknown database ‘xxxxx‘
Redefine analysis - release of eventbridge real-time event analysis platform
Focus loss explanation
BigDecimal common API
Spreadsheet export excel table
华为回应美国封锁供应链:他们仍需为5G专利付费
Use of swarm task task