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

StringsubstringAfterLast(String text, char after)
Returns the portion of the overall string that comes after the last occurance of the given string.
int indexOf = text.lastIndexOf(after);
if (indexOf == -1) {
    return text;
return text.substring(indexOf + 1);
StringsubStringAfterLastSymbol(String inStr, char symbol)
sub String After Last Symbol
return inStr.substring(inStr.lastIndexOf(symbol) + 1);
StringsubstringAfterReturnAll(String str, String before)
substring After Return All
int pos = str.indexOf(before);
if (pos != -1) {
    return str.substring(pos + before.length());
return str;
StringsubstringAndchopLastNewline(String text, int start_pos, int end_pos)
Gets text substring from 'start_pos' position till 'end_pos' position and chop last symbol if it is newline \n symbol.
if (start_pos < 0 || start_pos >= end_pos || end_pos > text.length() - 1) {
    return NULL_STRING;
if (end_pos > 0 && '\n' == text.charAt(end_pos - 1)) {
    end_pos--;
return text.substring(start_pos, end_pos);
String[]subStringArray(String in[], int start, int end)
Return a subset of an array.
String[] ret = new String[end - start];
int j = 0;
for (int i = start; i < end; i++) {
    ret[j++] = in[i];
return ret;
StringsubstringBaseName(String baseName, int extraCharsNumber)
substring Base Name
baseName = baseName.substring(0, baseName.length() - extraCharsNumber - 3) + "...";
return baseName;
StringsubstringBefore(final String str, final String separator)
substring Before
if (str == null || str.length() == 0) {
    return str;
if (separator == null || str.indexOf(separator) == -1) {
    return str;
return str.substring(0, str.indexOf(separator));
StringsubstringBefore(final String string, final String separator)
substring Before
if (isEmpty(string) || separator == null) {
    return string;
if (separator.length() == 0) {
    return "";
final int pos = string.indexOf(separator);
if (pos == -1) {
...
StringsubstringBefore(String input, String sub)
Return the substring before the first occurrence of SUB else INPUT (if substring does not occur).
int pos = input.indexOf(sub);
if (pos < 0) {
    return input;
return input.substring(0, pos);
StringsubstringBefore(String s, char c)
substring Before
int pos = s.indexOf(c);
return pos >= 0 ? s.substring(0, pos) : s;