Java String Find findParam(String src, char patternFrom, char patternTo)

Here you can find the source of findParam(String src, char patternFrom, char patternTo)

Description

return the parameter list.

License

Apache License

Parameter

Parameter Description
src a parameter
patternFrom a parameter
patternTo a parameter

Declaration

public static String[] findParam(String src, char patternFrom, char patternTo) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/* www  .ja v  a 2  s  .co  m*/
     * return the parameter list.
     * findParam("Ther versionId is {versionId}, created by {userId}.",'{','}')
     * will return ["{versionId}","{userId}"]
     *
     * @param src
     * @param patternFrom
     * @param patternTo
     * @return
     */
    public static String[] findParam(String src, char patternFrom, char patternTo) {
        ArrayList list = new ArrayList();
        int i, j = 0, c = 0;
        while (c < src.length()) {
            i = src.indexOf(patternFrom, c);
            if (i != -1) {
                j = src.indexOf(patternTo, i + 1);
                if (j != -1) {
                    list.add(src.substring(i, j + 1));
                    c = j + 1;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        String[] result = new String[list.size()];
        for (i = 0; i < list.size(); i++) {
            result[i] = (String) list.get(i);
        }
        return result;
    }

    /**
     * Find the index of the searchStr in the string array strs.
     * Return -1, if not found.
     *
     * <pre>
     * StringUtil.indexOf(["s1", "s2"], "s1", true) = 0
     * StringUtil.indexOf(["s1", "s2"], "S1", true) = -1
     * </pre>
     *
     * @param strs
     *            the string array to check, maybe null.
     * @param searchStr
     *            the string to search for, maybe null.
     * @param caseSensive
     *            is it case sensive while finding the searchStr
     *            in the searchStr.
     * @return index of the searchStr found in the strs, -1 if not found.
     *
     * @author chenke
     */
    public static int indexOf(String[] strs, String searchStr, boolean caseSensive) {

        if (strs == null || strs.length == 0) {
            return -1;
        }
        for (int i = 0; i < strs.length; i++) {
            if (isEqual(strs[i], searchStr, caseSensive)) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(String[] strs, String searchStr) {
        return indexOf(strs, searchStr, true);
    }

    private static boolean isEqual(String s1, String s2, boolean caseSensive) {
        if (caseSensive) {
            return s1 == null ? s2 == null : s1.equals(s2);
        } else {
            return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
        }
    }
}

Related

  1. findAll(String toSearch, String toFind, boolean ignoreCase)
  2. findAllIndexes(String str, String searchStr)
  3. findAllOccurences(String str, String pattern)
  4. findAllOccurrences(String str, String substr)
  5. findAllSubsequences(String str)
  6. findResourceBundle(String aBundleName, Locale locale)
  7. getAllOccurences(String str, char guess)
  8. getOccurenceIndices(String inSubject, String inOccurence)
  9. getSearchTermOccurrences(final String searchTerm, final String content)