Here you can find the source of getOccurenceIndices(String inSubject, String inOccurence)
public static List<Integer> getOccurenceIndices(String inSubject, String inOccurence)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { public static List<Integer> getOccurenceIndices(String inSubject, String inOccurence) { if (inSubject == null || inOccurence == null) { return new ArrayList<Integer>(0); }//from w w w . j a v a 2 s . c o m final List<Integer> theOccurences = new ArrayList<Integer>(); int theLastIndex = 0; while (inSubject.length() >= theLastIndex + inOccurence.length()) { theLastIndex = inSubject.indexOf(inOccurence, theLastIndex); if (theLastIndex < 0) { break; } theOccurences.add(theLastIndex); theLastIndex += inOccurence.length(); } return theOccurences; } }