Java String Sub String substring(String str, int start, int end)

Here you can find the source of substring(String str, int start, int end)

Description

Find substring of a string.

License

Open Source License

Parameter

Parameter Description
str The string.
start Starting character position (0 origin) of substring
end Ending character + 1 position of substring.

Return

The substring. Empty string if input is null or the substring doesn't exist.

Works like usual String class substring, but handles problem values for start/end values without throwing exceptions.

Declaration


public static String substring(String str, int start, int end) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

public class Main {
    /**/*from  w ww.j  a  v a2s.  com*/
     * Find substring of a string.
     * 
     * @param str
     *            The string.
     * @param start
     *            Starting character position (0 origin) of substring
     * @param end
     *            Ending character + 1 position of substring.
     * 
     * @return The substring. Empty string if input is null or the substring
     *         doesn't exist.
     * 
     *         <p>
     *         Works like usual String class substring, but handles problem
     *         values for start/end values without throwing exceptions.
     *         </p>
     */

    public static String substring(String str, int start, int end) {
        String result = "";

        if (str != null) {
            try {
                result = str.substring(start, end);
            } catch (Exception e) {
            }
        }

        return result;
    }
}

Related

  1. subString(String str, int offset, int leng)
  2. substring(String str, int start)
  3. substring(String str, int start)
  4. substring(String str, int start)
  5. substring(String str, int start, int end)
  6. subString(String str, int start, int end)
  7. substring(String str, int toCount)
  8. substring(String str, int toCount, String more)
  9. substring(String str, String delim)