Java String Find findAll(String toSearch, String toFind, boolean ignoreCase)

Here you can find the source of findAll(String toSearch, String toFind, boolean ignoreCase)

Description

find All

License

Open Source License

Declaration

public static List<Integer> findAll(String toSearch, String toFind, boolean ignoreCase) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .  j  a  v  a  2s. c  o  m*/
 * Copyright (c) 2004-2012 The YAWL Foundation. All rights reserved.
 * The YAWL Foundation is a collaboration of individuals and
 * organisations who are committed to improving workflow technology.
 *
 * This file is part of YAWL. YAWL is free software: you can
 * redistribute it and/or modify it under the terms of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 *
 * YAWL is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with YAWL. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    public static List<Integer> findAll(String toSearch, String toFind, boolean ignoreCase) {
        if (ignoreCase) {
            if (!((toSearch == null) || (toFind == null))) {
                toSearch = toSearch.toUpperCase();
                toFind = toFind.toUpperCase();
            }
        }
        return findAll(toSearch, toFind);
    }

    public static List<Integer> findAll(String toSearch, String toFind) {
        List<Integer> foundList = new ArrayList<Integer>();
        int start = 0;
        while (start > -1) {
            start = find(toSearch, toFind, start);
            if (start > -1)
                foundList.add(start++);
        }
        return foundList;
    }

    public static int find(String toSearch, String toFind, int start, boolean ignoreCase) {
        if ((toSearch == null) || (toFind == null))
            return -1;
        if (ignoreCase) {
            toSearch = toSearch.toUpperCase();
            toFind = toFind.toUpperCase();
        }
        return find(toSearch, toFind, start);
    }

    public static int find(String toSearch, String toFind, int start) {
        if ((toSearch == null) || (toFind == null))
            return -1;
        if ((toSearch.length() < 2048) || (toFind.length() < 4)) {
            return toSearch.indexOf(toFind, start);
        }
        if (start < 0)
            start = 0;
        final int lastCharToFindIndex = toFind.length() - 1;
        final char lastCharToFind = toFind.charAt(toFind.length() - 1);

        int[] skipTable = new int[255];
        for (int i = 0; i < 255; i++)
            skipTable[i] = toFind.length();
        for (int i = 0; i < lastCharToFindIndex; i++) {
            skipTable[toFind.charAt(i) & 255] = lastCharToFindIndex - i;
        }

        for (int i = start + lastCharToFindIndex; i < toSearch.length(); i += skipTable[toSearch.charAt(i) & 255]) {

            if (toSearch.charAt(i) != lastCharToFind) {
                while ((i += skipTable[toSearch.charAt(i) & 255]) < toSearch.length()
                        && toSearch.charAt(i) != lastCharToFind)
                    ;

                if (i < toSearch.length()) {
                    int j = i - 1;
                    int index = i - toFind.length() + 1;
                    for (int k = lastCharToFindIndex - 1; j > index
                            && toSearch.charAt(j) == toFind.charAt(k); j--, k--)
                        ;

                    if (j == index)
                        return index;
                } else
                    break;
            }
        }
        return -1;
    }

    public static int find(String toSearch, String toFind) {
        return find(toSearch, toFind, 0);
    }
}

Related

  1. findAll(final String s, final String subs)
  2. findAllIndexes(String str, String searchStr)
  3. findAllOccurences(String str, String pattern)
  4. findAllOccurrences(String str, String substr)
  5. findAllSubsequences(String str)