Java Utililty Methods String Chop

List of utility methods to do String Chop

Description

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

Method

StringchopId(String name)
chop Id
int id1in = name.indexOf('-');
String id1s = name.substring(id1in + 1);
return id1s;
voidchopIfMatch(StringBuilder sb, char ch)
chop If Match
if (sb.length() != 0 && sb.charAt(sb.length() - 1) == ch)
    sb.setLength(sb.length() - 1);
StringchopLast(String value, int chars)
CHOP LAST
if (chars < 0 || chars > value.length())
    throw new IllegalArgumentException("invalid chars: " + chars + " / " + value.length());
return value.substring(0, value.length() - chars);
StringchopLenghtyString(String title, int maxLen)
When the title is really long and without whitespaces it really messes up the ui.
if (isBreakNeeded(title, maxLen)) {
    title = chopString(title, maxLen);
return title;
StringchopLongLinesAtSpaces(int maxLineLenght, String text)
chop Long Lines At Spaces
StringBuilder result = new StringBuilder();
int currentIndex = 0;
while (currentIndex < text.length()) {
    int newLineIdx = text.indexOf("\n", currentIndex);
    String line;
    if (newLineIdx == -1) {
        line = text.substring(currentIndex);
        currentIndex = text.length();
...
StringchopNull(String str)
chop Null
if (str == null) {
    return "";
} else {
    return str;
StringchopPrefix(String x, String prefix)
chop Prefix
if (x.startsWith(prefix))
    return x.substring(prefix.length());
else
    return null;
StringChopRt(String this_string, String chomp_off)
Chop Rt
if (!this_string.contains(chomp_off))
    return this_string;
return this_string.substring(0, this_string.lastIndexOf(chomp_off));
StringchopScheme(String path)
Chops the scheme/protocol from the given URL path and returns the path without the scheme.
int idx = path.indexOf(":");
if (idx >= 0) {
    String toReturn = path.substring(idx + 1);
    if (toReturn.startsWith("//")) {
        toReturn = toReturn.substring(1);
    return toReturn;
return path;
StringchopScheme(String path)
Chops the scheme/protocol from the given URL path and returns the path without the scheme.
int idx = path.indexOf(":");
if (idx >= 0) {
    if (path.length() >= 2) {
        if (path.charAt(1) == ':') {
            if (Character.isLetter(path.charAt(0))) {
                return path;
    String toReturn = path.substring(idx + 1);
    if (toReturn.startsWith("//")) {
        toReturn = toReturn.substring(1);
    return toReturn;
return path;