Java String Sub String substring(final String pSource, final String pBeginBoundaryString, final String pEndBoundaryString, final int pOffset)

Here you can find the source of substring(final String pSource, final String pBeginBoundaryString, final String pEndBoundaryString, final int pOffset)

Description

Gets the first substring between the given string boundaries.

License

Open Source License

Parameter

Parameter Description
pSource The source string.
pBeginBoundaryString The string that marks the beginning.
pEndBoundaryString The string that marks the end.
pOffset The index to start searching in the source string. If it is less than 0, the index will be set to 0.

Return

the substring demarcated by the given string boundaries or null if not both string boundaries are found.

Declaration

public static String substring(final String pSource, final String pBeginBoundaryString,
        final String pEndBoundaryString, final int pOffset) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  ww  w .ja va2s .c o  m
     * Gets the first substring between the given string boundaries.
     * <p/>
     *
     * @param pSource              The source string.
     * @param pBeginBoundaryString The string that marks the beginning.
     * @param pEndBoundaryString   The string that marks the end.
     * @param pOffset              The index to start searching in the source
     *                             string. If it is less than 0, the index will be set to 0.
     * @return the substring demarcated by the given string boundaries or null
     *         if not both string boundaries are found.
     */
    public static String substring(final String pSource, final String pBeginBoundaryString,
            final String pEndBoundaryString, final int pOffset) {
        // Check offset
        int offset = (pOffset < 0) ? 0 : pOffset;

        // Find the start index
        int startIndex = pSource.indexOf(pBeginBoundaryString, offset) + pBeginBoundaryString.length();

        if (startIndex < 0) {
            return null;
        }

        // Find the end index
        int endIndex = pSource.indexOf(pEndBoundaryString, startIndex);

        if (endIndex < 0) {
            return null;
        }
        return pSource.substring(startIndex, endIndex);
    }
}

Related

  1. substrCount(String haystack, String needle)
  2. subStrIfNeed(final String str, int num)
  3. substring(byte[] array, int start)
  4. substring(byte[] src, int start, int len)
  5. substring(char[] s, int start, int end)
  6. substring(final String s, int start)
  7. substring(final String s, int start, int end)
  8. substring(final String s, int startIndex, int endIndex)
  9. substring(final String str, int start, int end)