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

边栏推荐
- 激光器芯片厂商陕西源杰半导体获广发证券、中信证券等8家投资机构入股
- 站在数字零售转型的十字路口,我们需要用新的角度来看待它
- Data problems can also be found if there is a space at the end of the field value of MySQL query criteria
- 4月全球智能手机出货同比下滑41%,华为首次超三星成全球第一
- mysql-JPA对数据库中JSON类型数据的支持
- mysql查询条件字段值末尾有空格也能查到数据问题
- Arm中国夺权大战的背后:“独立”两年,仍难“自主”?
- Three basic teaching
- Gazebo 控制实例
- BAT大厂测试架构师如何解读测试平台的各种争议
猜你喜欢

ABAP CDS Table Function介绍与示例

Three instance

Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used

Software process that testers need to know

Redis cache penetration breakdown and avalanche

Cesium add dynamic pop-up

Codeforces暑期训练周报(7.14~7.20)

idea常用的快捷键汇总

Introduction to the browser video frame operation method requestvideoframecallback()

Principle of logistic regression
随机推荐
Adding custom dynamic arts and Sciences to cesium
Byte monthly salary 28K, share a wave of my automation testing experience
华为回应美国封锁供应链:他们仍需为5G专利付费
Advanced MySQL -- stored procedures and custom functions
BigDecimal common API
Principle of logistic regression
mysql-JPA对数据库中JSON类型数据的支持
ABAP CDs table function introduction and examples
oracle分组取最大值
Huawei responded to the US blockade of the supply chain: they still have to pay for 5g patents
Use of swarm task task
重新定义分析 - EventBridge 实时事件分析平台发布
Jingfang Technology: ASML, a lithography machine manufacturer, is one of the main customers of Anterion company, which participated in the merger and acquisition of the company
容器网络硬核技术内幕 (7) 大海航行靠舵手
安全检测风险
二维数组相关知识
Fabric2.4.4 version building process (complete process)
美光起诉联电窃密案宣判:联电被罚1亿元新台币,三名员工被判刑!
C语言main函数传递参数
Operator depth anatomy