Java Utililty Methods String Sub String

List of utility methods to do String Sub String

Description

The list of methods to do String Sub String are organized into topic(s).

Method

Stringsubstring(String src, String fromToken, String toToken)
substring
int from = src.indexOf(fromToken);
if (from == -1)
    return null;
from += fromToken.length();
int to = src.indexOf(toToken, from);
if (to == -1) {
    return src.substring(from);
return src.substring(from, to);
StringsubString(String src, String start, String to)
Example: subString("abcd","a","c")="b"
int indexFrom = start == null ? 0 : src.indexOf(start);
int indexTo = to == null ? src.length() : src.indexOf(to);
if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
    return null;
if (null != start) {
    indexFrom += start.length();
return src.substring(indexFrom, indexTo);
StringsubString(String src, String start, String to)
Example: subString("abcd","a","c")="b"
int indexFrom = start == null ? 0 : src.indexOf(start);
int indexTo = to == null ? src.length() : src.indexOf(to);
if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
    return null;
if (null != start) {
    indexFrom += start.length();
return src.substring(indexFrom, indexTo);
StringsubString(String srcStr, int subLen)

Description:Sub a string with the length,

byte[] bytes = srcStr.getBytes();
if (bytes.length <= subLen)
    return srcStr;
char[] chars = srcStr.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0, j = 0; i < subLen && i < bytes.length && j < chars.length;) {
    char cbyte = (char) bytes[i];
    if (cbyte == chars[j]) {
...
Stringsubstring(String str, char pattern)
substring
int pos1 = -1, pos2 = -1;
for (int j = 0; j < str.length(); j++) {
    if (str.charAt(j) == pattern) {
        if (pos1 == -1)
            pos1 = j;
        else {
            pos2 = j;
            break;
...
Stringsubstring(String str, int beginIndex)
same as String.substring, except that this version handles the case robustly when the index is out of bounds.
if (str == null)
    return null;
if (beginIndex < 0)
    return str;
if (beginIndex >= str.length())
    return "";
return str.substring(beginIndex);
StringsubString(String str, int beginIndex, int endIndex)
sub String
return substring(str, beginIndex, endIndex);
Stringsubstring(String str, int beginIndex, int endIndex)
Index safe substring operation.
if (beginIndex < 0) {
    beginIndex = 0;
if (endIndex > str.length()) {
    endIndex = str.length();
return str.substring(beginIndex, endIndex);
Stringsubstring(String str, int beginIndex, int endIndex)
substring
if (str == null)
    return "";
int length = str.length();
endIndex = (endIndex < 0) ? 0 : endIndex;
endIndex = (endIndex > length) ? length : endIndex;
beginIndex = (beginIndex < 0) ? 0 : beginIndex;
beginIndex = (beginIndex > endIndex) ? endIndex : beginIndex;
str = str.substring(beginIndex, endIndex);
...
Stringsubstring(String str, int beginIndex, int len)
substring
int endIndex = beginIndex + len;
endIndex = endIndex > str.length() ? str.length() : endIndex;
return str.substring(beginIndex, endIndex);