Java String Sub String substring(final String s, int startIndex, int endIndex)

Here you can find the source of substring(final String s, int startIndex, int endIndex)

Description

a 'safe' @link String#substring(int,int) that does not throw exceptions when indexes are false.

License

Open Source License

Parameter

Parameter Description
s any string
startIndex start index (maybe greater that endIndex)
endIndex required end index (may greater that the string itself)

Return

substring

Declaration

public static String substring(final String s, int startIndex, int endIndex) 

Method Source Code

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

public class Main {
    /**//from   www .  j a v  a2  s  .c  om
     * a 'safe' @link {@link String#substring(int, int)} that does not throw exceptions when indexes are false.
     *
     * @param s
     *            any string
     * @param startIndex
     *            start index (maybe greater that endIndex)
     * @param endIndex
     *            required end index (may greater that the string itself)
     * @return substring
     */
    public static String substring(final String s, int startIndex, int endIndex) {
        startIndex = Math.min(s.length(), Math.max(0, startIndex));
        endIndex = Math.min(s.length(), Math.max(0, endIndex));
        return s.substring(Math.min(startIndex, endIndex), Math.max(startIndex, endIndex));
    }
}

Related

  1. substring(byte[] src, int start, int len)
  2. substring(char[] s, int start, int end)
  3. substring(final String pSource, final String pBeginBoundaryString, final String pEndBoundaryString, final int pOffset)
  4. substring(final String s, int start)
  5. substring(final String s, int start, int end)
  6. substring(final String str, int start, int end)
  7. substring(final String string, int fromIndex, int toIndex)
  8. substring(final String text, final int position, final int length)
  9. substring(String _text, int _idx)