如果一个字符串在另一个字符串中出现了多次,怎么能获得它出现的所有位置?
怎么实现效率会比较好?

解决方案 »

  1.   


    public class StringTest { public static void main(String args[]) {
    String s = "hello, this is a test this is a test this is a this is test";

    String p = "test";
    for(int i = 0;  i < s.length(); i ++) {
    int at = s.indexOf(p, i);

    if(at < 0) {
    break;
    }

    System.out.println(p + "@" + at);
    i = at;
    }
    }
    }