java查找字符串中的包含子字符串的个数实现代码
作者:jingxian 时间:2022-08-15 17:48:45
1. 用indexof的方法:
public class Test11
{
private static int counter = 0;
/**
* @param args
*/
public static void main(String[] args)
{
String str ="sdSS**&HGJhadHCASch& ^^";
int i = stringNumbers(str);
System.out.println(i);
}
public static int stringNumbers(String str)
{
if (str.indexOf("java")==-1)
{
return 0;
}
else if(str.indexOf("java") != -1)
{
counter++;
stringNumbers(str.substring(str.indexOf("java")+4));
return counter;
}
return 0;
}
}
2. 如果子字符串不是一个首尾相同的字符串,还可以这样实现:
if(str.indexOf("java") != -1)
{
String[] str1 = str.split("java");
System.out.println(str1.length-1);
}
else
{
System.out.println(0);
}
标签:java,字符串,子字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于Java中修饰符的总结(fina除外)
2023-11-22 23:15:57
使用java实现http多线程断点下载文件(二)
2021-06-08 04:26:17
Java实战之用hutool-db实现多数据源配置
2023-11-28 19:37:10
![](https://img.aspxhome.com/file/2023/0/60250_0s.png)
c#实现多线程局域网聊天系统
2022-12-01 23:34:25
Spring MVC 简单的hello world的实现
2023-06-18 02:56:41
![](https://img.aspxhome.com/file/2023/7/72597_0s.png)
Spring中propagation的7种事务配置及说明
2023-01-02 09:47:26
![](https://img.aspxhome.com/file/2023/9/70539_0s.jpg)
c#和java base64不一致的解决方法
2022-11-24 02:18:55
![](https://img.aspxhome.com/file/2023/0/68740_0s.png)
Java synchronized锁升级jol过程详解
2023-04-15 04:58:51
![](https://img.aspxhome.com/file/2023/4/61864_0s.jpg)
Java多线程-线程的同步与锁的问题
2023-11-29 01:40:12
Java泛型的使用限制实例分析
2023-05-07 20:14:52
spring mvc中直接注入的HttpServletRequst安全吗
2021-12-29 07:48:16
![](https://img.aspxhome.com/file/2023/6/63506_0s.png)
C#端口转发用法详解
2022-09-05 08:47:08
IDEA中的.iml文件和.idea文件夹
2023-11-23 11:47:19
![](https://img.aspxhome.com/file/2023/0/59680_0s.png)
关于Mybatis插入对象时空值的处理
2023-08-28 21:58:00
Android View移动的六种方法小结
2023-07-06 02:43:39
Spring和SpringBoot之间的区别
2022-09-28 11:47:38
C#使用Objects Comparer进行对象比较
2022-06-03 05:40:30
![](https://img.aspxhome.com/file/2023/7/70407_0s.jpg)
Android实现悬浮窗的简单方法实例
2023-06-17 18:11:02
Java实现驼峰、下划线互转的方法
2023-08-18 09:17:54
Java多线程之Worker Thread模式
2021-06-11 12:11:51