Pattern pa=Pattern.compile(",");
String exs = ",,,,";
Matcher ma = pa.matcher(exs);
int i=0;
while(ma.find()){
   i++;
}
System.out.println(i);

解决方案 »

  1.   

    String sd="this , is, a ,test";
    String str[];
    str=sd.split(",");
    System.out.println(str.length);//几个子串组成
    System.out.println(str.length-1);//几个","号
      

  2.   

    不知道楼主是要效率高还是要方便的。方便的可以这样:String[] s1 = ",,,,,".ReplaceAll(",",",a").split(",");
    s1.length - 1即可
      

  3.   

    thomas_20(执子之手,与子偕老) 的方法应该是不错的,
    但是Pattern pa=Pattern.compile(",")这句话里面的","换成"."之后竟然是一样的结果(eclipse3.0.1+jdk1.4.2),我不是很明白,请thomas_20兄说明一下Pattern 和Matcher 这两个东西,谢谢
      

  4.   

    我给你一个例子,java2编程指南上面的。
    public class ExtractSubstring
    {
      public static void main(String[] args)
      {
        String text = "i like csdn";        
        int count = 0;                             
        char separator = ' ';                      
        int index = 0;
        do
        {
          ++count;                           
          ++index;                                 
          index = text.indexOf(separator, index);
        }
        while (index != -1);    
        String[] subStr = new String[count];       
        index = 0;                                 
        int endIndex = 0;                          
        for(int i = 0; i < count; i++)
        {
          endIndex = text.indexOf(separator,index);        if(endIndex == -1)                       
            subStr[i] = text.substring(index);     
          else                                             
            subStr[i] = text.substring(index, endIndex);         index = endIndex + 1;                    
        }    // Display the substrings
        for(int i = 0; i < subStr.length; i++)
          System.out.println(subStr[i]);
      }
    }
      

  5.   

    String s1 = ",,,,,";
    int strs = s1.length()-s1.replaceAll(",","").length();这个够快了吧,把所有的","都砍了,用原来的长度减一减就出来了
      

  6.   


    楼上这个兄弟就写的很好.
    febchen() ( ) 信誉:105  2004-12-13 14:29:00  得分: 0  
     
     
       String sd="this , is, a ,test";
    String str[];
    str=sd.split(",");
    System.out.println(str.length);//几个子串组成
    System.out.println(str.length-1);//几个","号
      
     
      

  7.   

    我应聘时的一道题目  
    有一string a
    还有一string b
    其中b包含在a里面    据个例子    a="hello"  b="el"
    要求 遍历 a字符串  然后把 a中所有的 b  全部反过来
    再举个 例子   a="hello"  b="el"    a中有一个b   那么就把el换为 le   最后输出   a="hlelo"
      

  8.   

    static String a = "hellohellohello";
    System.out.println(a.replaceAll("el","le"));
      

  9.   

    String s1 = ",,,,,";
    int strs = s1.length()-s1.replaceAll(",","").length();
    这个最好了,我看。
      

  10.   

    如果找",," 就不对了.String s1 = "1,,2,3,4,5,,6";
    int strs = s1.length()-s1.replaceAll(",,","").length();
    System.out.println( strs );
      

  11.   

    String strFindSub = "232"; // 要找的Substring...
    String s1 = "1,,223,42,56,22";
    int strs = ( s1.length()-s1.replaceAll(strFindSub,"").length() ) / strFindSub.length();
    System.out.println( strs );一般情况下这样估计就可以了...但是如果我要找  "\\"  ,  "\" .....等怎么办............
      

  12.   

    static int count(String str,String sub){
    int index=str.indexOf(sub);
    int len=sub.length();
    int cnt=0;
    while (index!=-1)
    {
    index+=len;
    index=str.indexOf(sub,index);
    cnt++;
    }

    return cnt;
    }