在字符串中找子串
来源:互联网 发布:沙漠之鹰数据图 编辑:程序博客网 时间:2024/05/29 12:24
public class StringFind {
public static void main(String []args){
String str="nbasfsdfnbadfdsnbagdfsgdfgnbagdfgdnbasnba";
String key="nba";
int count=getFindString(str,key);
int count2=getFindString_2(str,key);
System.out.println(count);
System.out.println(count2);
}
//方法一
public static int getFindString(String str, String key) {
//计数器
int count=0;
//记录key出现的位置
int index=0;
//内存中有字符串不断生成字符串常量
while((index=str.indexOf(key))!=-1){
str=str.substring(index+key.length());
count++;
}
return count;
}
//方法二
public static int getFindString_2(String str, String key) {
int count=0;
int index=0;
while((index=index=str.indexOf(key,index))!=-1){
index=index+key.length();
count++;
}
return count;
}
}
public static void main(String []args){
String str="nbasfsdfnbadfdsnbagdfsgdfgnbagdfgdnbasnba";
String key="nba";
int count=getFindString(str,key);
int count2=getFindString_2(str,key);
System.out.println(count);
System.out.println(count2);
}
//方法一
public static int getFindString(String str, String key) {
//计数器
int count=0;
//记录key出现的位置
int index=0;
//内存中有字符串不断生成字符串常量
while((index=str.indexOf(key))!=-1){
str=str.substring(index+key.length());
count++;
}
return count;
}
//方法二
public static int getFindString_2(String str, String key) {
int count=0;
int index=0;
while((index=index=str.indexOf(key,index))!=-1){
index=index+key.length();
count++;
}
return count;
}
}
0 0
- 在字符串中找子串
- 在字符串中查找字符串
- 在字符串中查找字符串
- 在字符串中查找另一个字符串
- 在一字符串中查找另一字符串
- 在字符串数组中搜索字符串
- 在字符串中随机插入字符串
- 判断字符串是否在另一个字符串中
- 在字符串中查找字符串问题
- 在字符串中寻找子字符串
- 在字符串中查找最长字符串
- 在字符串中查找字符串 Strstr() StrstrSpan()
- 在一个字符串中提取子字符串
- strstr:在字符串中寻找子字符串
- 在字符串中的特定位置插入字符串
- 在字符串中查找中文字符串
- 在字符串指定位置插入字符串
- 在指定字符串后面插入字符串
- OC 基本知识点总结
- Android 控件之十二:Gallery画廊
- VMware ESX与VMware ESXi区别
- 使用线程的几种方式(三)客户/服务器
- IOS--UIButton的使用方法详细
- 在字符串中找子串
- IOS--UIAlertView的使用方法详细
- hdu 4289 Control【最小割+拆点】
- 浙江大学PAT_甲级_1062. Talent and Virtue (25)
- NBUT 1223 Friends number
- IOS--UITextFiled的使用方法详细
- uvalive5090(三维凸包)
- Xcode常用快捷键
- Android Studio 快捷键