for example:String test = "1,3,10,6,25,40,9";怎样得到第n个","在整个string里面的index?

解决方案 »

  1.   

    sorry,我是想问能否用正则表达式做出来.
      

  2.   

    public static void main(String[] args) {
    StringFind sf = new StringFind();
    System.out.println(sf.find(5));
    }

    public int find(int n) {
    String test = "1,3,10,6,25,40,9";

    Pattern p = Pattern.compile(",");
    Matcher m = p.matcher(test);
    int i = 0;
    while (m.find()) {
    i++;
    if (n == i) 
    return m.start();
    }
    return -1;
    }楼主想要这样的吗?
    你要记得给分啊