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

StringsubstringBeforeLast(String str, String separator)

Gets the substring before the last occurrence of a separator.

if (isEmpty(str) || isEmpty(separator)) {
    return str;
int pos = str.lastIndexOf(separator);
if (pos == -1) {
    return str;
return str.substring(0, pos);
...
StringsubstringBeforeLast(String str, String separator)
substring Before Last
if (str == null || separator == null || str.length() == 0 || separator.length() == 0) {
    return str;
int pos = str.lastIndexOf(separator);
if (pos == -1) {
    return str;
return str.substring(0, pos);
...
StringsubStringBeforeLast(String str, String separator)
returns string before last entry of separator in case string doesn't contain separator -- returns whole string
int from = str.lastIndexOf(separator);
return from == -1 ? str : str.substring(0, from);
StringsubstringBeforeLast(String text, String separator)
substring Before Last
int index = lastIndexOf(text, separator);
return substringBefore(text, index);
StringsubstringBeforeLastChar(String str, String separator)
substring Before Last Char
if (isEmpty(str) || isEmpty(separator)) {
    return str;
int pos = str.lastIndexOf(separator);
if (pos == INDEX_NOT_FOUND) {
    return str;
if (pos == 0) {
...
StringsubstringBeforeLastIgnoreCase(final String target, final String separator)
substring Before Last Ignore Case
if ("".equals(target))
    return target;
if ("".equals(separator))
    return target;
final String tempStr = target.toUpperCase();
final String tempSeparator = separator.toUpperCase();
final int index = tempStr.lastIndexOf(tempSeparator);
if (index == -1)
...
StringsubstringBetween(final String s, final String tag)

Gets the String that is nested in between two instances of the same String.

return substringBetween(s, tag, tag);
StringsubstringBetween(final String str, final String startToken, final String endToken)
substring Between
int firstToken = str.indexOf(startToken) + 1;
int secondToken = str.indexOf(endToken, firstToken);
firstToken = firstToken < 0 ? 0 : firstToken;
secondToken = secondToken < 0 ? 0 : secondToken;
return str.substring(firstToken, secondToken);
StringsubstringBetween(String line, final String delimiterBefore, final String delimiterAfter)
substring Between
final int indexAfterFirstFromChar = line.indexOf(delimiterBefore) + 1;
final int firstIndexOfToChar = line.indexOf(delimiterAfter, indexAfterFirstFromChar);
return line.substring(indexAfterFirstFromChar, firstIndexOfToChar);
StringsubstringBetween(String s, String part1, String part2)
Returns the first instance of String found exclusively between part1 and part2.
String sub = null;
int i = s.indexOf(part1);
int j = s.indexOf(part2, i + part1.length());
if (i != -1 && j != -1) {
    int nStart = i + part1.length();
    sub = s.substring(nStart, j);
return sub;
...