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

byte[]substring(byte[] array, int start)
substring
return substring(array, start, array.length);
byte[]substring(byte[] src, int start, int len)
substring
byte[] res = new byte[len];
System.arraycopy(src, start, res, 0, len);
return res;
char[]substring(char[] s, int start, int end)
substring
char[] result = new char[end - start];
for (int i = 0; i < result.length; i++) {
    result[i] = s[start + i];
return result;
Stringsubstring(final String pSource, final String pBeginBoundaryString, final String pEndBoundaryString, final int pOffset)
Gets the first substring between the given string boundaries.
int offset = (pOffset < 0) ? 0 : pOffset;
int startIndex = pSource.indexOf(pBeginBoundaryString, offset) + pBeginBoundaryString.length();
if (startIndex < 0) {
    return null;
int endIndex = pSource.indexOf(pEndBoundaryString, startIndex);
if (endIndex < 0) {
    return null;
...
Stringsubstring(final String s, int start)

Gets a substring from the specified String avoiding exceptions.

if (s == null) {
    return null;
if (start < 0) {
    start = s.length() + start; 
if (start < 0) {
    start = 0;
...
Stringsubstring(final String s, int start, int end)
Equal to String#substring(int,int) , but allows negative indices that are counted from the end of the string.
if (start < 0)
    start = start + s.length();
if (end < 0)
    end = end + s.length();
if (end < start)
    throw new IllegalArgumentException("invalid indices");
return "" + s.substring(start, end);
Stringsubstring(final String s, int startIndex, int endIndex)
a 'safe' @link String#substring(int,int) that does not throw exceptions when indexes are false.
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));
Stringsubstring(final String str, int start, int end)
From commonslang3 -> StringUtil

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start/end n characters from the end of the String.

The returned substring starts with the character in the start position and ends before the end position.

if (str == null) {
    return null;
if (end < 0) {
    end = str.length() + end; 
if (start < 0) {
    start = str.length() + start; 
...
Stringsubstring(final String string, int fromIndex, int toIndex)
substring
final int len = string.length();
if (fromIndex < 0) {
    fromIndex += len;
    if (toIndex == 0) {
        toIndex = len;
if (toIndex < 0) {
...
Stringsubstring(final String text, final int position, final int length)
Return substring of text.
final int start = Math.max(0, position);
int l = position + length - start;
if (l <= 0) {
    return "";
int end = start + l;
if (end < text.length()) {
    return text.substring(start, end);
...