Here you can find the source of subString(String string, int beginIndex, int length)
Parameter | Description |
---|---|
string | the string. |
beginIndex | the zero-based begin index. |
length | the length of the sub string starting at the begin index. |
public static String subString(String string, int beginIndex, int length)
//package com.java2s; public class Main { public static final String EMPTY = ""; /**/*from w ww .j a v a 2s . co m*/ * Gets the sub string of the given string. If the beginIndex is larger than * the length of the string, the empty string is returned. If the beginIndex + * the length is larger than the length of the string, the part of the string * following the beginIndex is returned. Method is out-of-range safe. * * @param string the string. * @param beginIndex the zero-based begin index. * @param length the length of the sub string starting at the begin index. * @return the sub string of the given string. */ public static String subString(String string, int beginIndex, int length) { final int endIndex = beginIndex + length; if (beginIndex >= string.length()) { return EMPTY; } if (endIndex > string.length()) { return string.substring(beginIndex, string.length()); } return string.substring(beginIndex, endIndex); } }