fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String result = "quick brown fox jumps over the lazy fox (yes, fox!)";
  13. String packageName = "fox";
  14. int lastIndex = 0;
  15. List<String> allOccurences = new ArrayList<String>();
  16. int count = 0;
  17. while(lastIndex != -1) {
  18. lastIndex = result.indexOf(packageName, lastIndex);
  19. if(lastIndex != -1) {
  20. String line = result.substring(lastIndex, lastIndex+packageName.length());
  21. allOccurences.add(line);
  22. count++;
  23. lastIndex += packageName.length();
  24. }
  25. }
  26. for (String s : allOccurences) {
  27. System.out.println(s);
  28. }
  29. }
  30. }
Success #stdin #stdout 0.08s 381184KB
stdin
Standard input is empty
stdout
fox
fox
fox