java中字符串查找一个字符串的个数几种方法
- 摘要:方法1publicstaticvoidmain(String[]args){Strings="asdfasdfasdfjaskdfjaksfjasdnbasdfsdanbakdjfkldsjfnba";inty=0;while(s.indexOf("nba")!=-1){y++;s=s.substring(s.indexOf("nba")+4);}System.out.println(y);}方法2publicstaticvoidmain(String[]args)
- 标签:方法 查找 Java 一个 字符串
方法1
public static void main(String[] args) {
String s = "asdfasdfasdfjaskdfjaksfjasdnbasdfsdanbakdjfkldsjfnba";
int y = 0;
while (s.indexOf("nba")!=-1){
y++;
s = s.substring(s.indexOf("nba")+4);
}
System.out.println(y);
}
方法2
public static void main(String[] args) {
String s = "asdfasdfasdfjaskdfjaksfjasdnbasdfsdanbakdjfkldsjfnba";
Pattern p=Pattern.compile("nba");
Matcher m=p.matcher(s);
int count = 0;
while(m.find()) {
count++;
}
System.out.println(count);
}