java小练习--获取abc字符串在整个字符串中出现的次数
2013-09-12 22:06
429 查看
在下面一行字符串中获取abc字符串在整个字符串中出现的次数。
"wabcerabctyabcuiabcabcqq"
思路:使用indexOf和substring();
源码如下:
"wabcerabctyabcuiabcabcqq"
思路:使用indexOf和substring();
源码如下:
public static void main(String[] args) { String s1 = "abcwabcerabctyabcuiabcabc"; String s2 = "abc"; int count = getCount(s1,s2); int count2 = getCount2(s1,s2); System.out.println("count = "+count); System.out.println("count2 = "+count2); } /*第一种方法 获取abc字符串在整个字符串中出现的次数。 "wabcerabctyabcuiabcabcqq" */ public static int getCount(String str,String sub) { int index = 0; int count = 0; while((index = str.indexOf(sub,index))!=-1) { index = index + sub.length(); count++; } return count; } /*第二种方法*/ public static int getCount2(String str,String sub) { int index = 0; int count = 0; while((index=str.indexOf(sub))!=-1) { str = str.substring(index+sub.length()); count++; } return count; } }
相关文章推荐
- java小练习--获取abc字符串在整个字符串中出现的次数
- java Map练习-获取字符串中字母出现次数(TreeMap实现)
- JAVA获取一个字符串在整个字符串中出现的次数
- Java 获取字符串中 每一个字母出现的次数
- Java 获取指定字符串出现的次数
- Java 获取指定字符串出现的次数
- Java 获取指定字符串出现的次数
- java String类-获取一个字符串在另一个字符串中出现的次数
- Java 获取指定字符串出现的次数
- java小练习之统计字符串中字符出现的次数
- Java 获取指定字符串出现的次数
- Android(Java) 字符串的常用操作,获取指定字符出现的次数,根据指定字符截取字符串
- 黑马程序员_集合TreeMap练习:获取某一字符串的字母出现的次数
- String小练习3-获取一个字符串在另一个字符串中出现的次数
- Java获取指定字符串出现次数的方法
- 【java编程】String之获取一个字符串在另一个字符串中出现的次数
- java面试题 "aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
- [Java练习]输入一个字符串,输出其中某个小写字母出现的次数
- JAVA 获取字符串中每一个字母出现的次数 TreeMap 解
- [Java练习]输入一个字符串,输出其中某个小写字母出现的次数