- /**
- * 取得big中子串sub的个数
- *
- * @param p1
- * @param p2
- * @return
- */
- public static int getSubstringNumber(String sub, String big); {
- String tmp = big;
- int num = 0;
-
- for (int i = 0; i < big.length();; i++); {
- if (tmp.indexOf(sub); != -1); {
- tmp = tmp.substring(tmp.indexOf(sub);+sub.length(););;
- num++;
- i = i + sub.length();;
- }
- System.out.println("i = "+i);;
- }
- return num;
- }
复制代码 新的写法: - import java.util.regex.*;
- private static int getSubstringNumber1(String d, String g); {
- Pattern p = null; //正则表达式
- Matcher m = null; //操作的字符串
- int i = 0;
- p = Pattern.compile(g);;
- m = p.matcher(d);;
- while (m.find();); {
- //m.appendReplacement(sb, "dog");;
- i++;
- }
- return i;
- }
复制代码- private static int getSubstringNumber1(String d, String g); {
- Pattern p = null; //正则表达式
- Matcher m = null; //操作的字符串
- int i = 0;
- p = Pattern.compile(g);;
- m = p.matcher(d);;
- StringBuffer sb = null;
- sb = new StringBuffer();;
- while (m.find();); {
- m.appendReplacement(sb, "dog");;
- i++;
- }
- System.out.println(sb);;
- return i;
- }
复制代码 |
|