Java String Find findAllOccurences(String str, String pattern)

Here you can find the source of findAllOccurences(String str, String pattern)

Description

Find all occurences (start positions) of pattern in string

License

Open Source License

Parameter

Parameter Description
str a parameter
pattern a parameter

Return

a list of all matched positions (starts)

Declaration

public static ArrayList<Integer> findAllOccurences(String str, String pattern) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    /**/* www.ja va2  s . co  m*/
     * Find all occurences (start positions) of pattern in string
     * @param str
     * @param pattern
     * @return a list of all matched positions (starts)
     */
    public static ArrayList<Integer> findAllOccurences(String str, String pattern) {
        ArrayList<Integer> pos = new ArrayList<Integer>();
        int len = pattern.length();
        if (len > 0) {
            int start = str.indexOf(pattern);
            while (start != -1) {
                pos.add(start);
                start = str.indexOf(pattern, start + len);
            }
        }
        return pos;
    }
}

Related

  1. findAll(final String s, final String subs)
  2. findAll(String toSearch, String toFind, boolean ignoreCase)
  3. findAllIndexes(String str, String searchStr)
  4. findAllOccurrences(String str, String substr)
  5. findAllSubsequences(String str)
  6. findParam(String src, char patternFrom, char patternTo)
  7. findResourceBundle(String aBundleName, Locale locale)