Java String Find indexOfOccurance(String s1, String s2, Integer i)

Here you can find the source of indexOfOccurance(String s1, String s2, Integer i)

Description

Index of occurance.

License

Apache License

Parameter

Parameter Description
s1 the s1
s2 the s2
i the i

Return

the int

Declaration

public static int indexOfOccurance(String s1, String s2, Integer i) 

Method Source Code


//package com.java2s;
/*/*from  w ww.  java 2 s  .c  o m*/
 * This file is part of the ContentServer project.
 * Visit the websites for more information. 
 *         - http://gdz.sub.uni-goettingen.de 
 *         - http://www.intranda.com 
 *         - http://www.digiverso.com
 * 
 * Copyright 2009, Center for Retrospective Digitization, G?ttingen (GDZ),
 * intranda software
 *
 * This is the extended version updated by intranda
 * Copyright 2012, intranda GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Index of occurance.
     * 
     * @param s1 the s1
     * @param s2 the s2
     * @param i the i
     * 
     * @return the int
     */
    public static int indexOfOccurance(String s1, String s2, Integer i) {
        ArrayList<Integer> al = new ArrayList<Integer>();
        al.addAll(allIndexOf(s1, s2));
        if (al.size() <= i - 1) {
            return al.get(i - i);
        } else {
            return -1;
        }
    }

    /**
     * Returns List of indexes within the given String of all occurrences of the specified character.
     * 
     * @param s1 the s1
     * @param s2 the s2
     * 
     * @return the List of indexes
     */
    public static List<Integer> allIndexOf(String s1, String s2) {
        List<Integer> al = new ArrayList<Integer>();
        StringBuffer sb = new StringBuffer(s1);
        Integer base = 0;
        while (sb.indexOf(s2) >= 0) {
            Integer pos = sb.indexOf(s2);
            al.add(pos + base);
            base += sb.delete(0, pos + s2.length()).length();
        }
        return al;
    }
}

Related

  1. findParam(String src, char patternFrom, char patternTo)
  2. findResourceBundle(String aBundleName, Locale locale)
  3. getAllOccurences(String str, char guess)
  4. getOccurenceIndices(String inSubject, String inOccurence)
  5. getSearchTermOccurrences(final String searchTerm, final String content)
  6. multiFindBetween(String source, String pre, String post)