Java Utililty Methods String Chomp

List of utility methods to do String Chomp

Description

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

Method

Stringchomp(String str)
chomp
if (isEmpty(str)) {
    return str;
if (str.length() == 1) {
    char ch = str.charAt(0);
    if ((ch == '\r') || (ch == '\n')) {
        return "";
    return str;
int lastIdx = str.length() - 1;
char last = str.charAt(lastIdx);
if (last == '\n') {
    if (str.charAt(lastIdx - 1) == '\r') {
        lastIdx--;
} else if (last != '\r') {
    lastIdx++;
return str.substring(0, lastIdx);
Stringchomp(String str)
Remove all whitespace from the start and end of a string.
if (str == null || str.length() == 0) {
    return str;
int firstIdx = 0;
int lastIdx = str.length() - 1;
char c = str.charAt(lastIdx);
while (c == '\n' || c == '\r' || c == '\t' || c == ' ') {
    if (lastIdx == 0) {
...
Stringchomp(String str)
chomp
return chomp(str, "\n");
Stringchomp(String str)
Removes last character if it is a newline.
if (str.endsWith("\n")) {
    return str.substring(0, str.length() - 1);
} else {
    return str;
Stringchomp(String str, String sep)

Remove the last value of a supplied String, and everything after it from a String.

int idx = str.lastIndexOf(sep);
if (idx != -1) {
    return str.substring(0, idx);
} else {
    return str;
Stringchomp(String str, String separator)

Removes separator from the end of str if it's there, otherwise leave it alone.

NOTE: This method changed in version 2.0.

if (isEmpty(str) || separator == null) {
    return str;
if (str.endsWith(separator)) {
    return str.substring(0, str.length() - separator.length());
return str;
Stringchomp(String value)
chomp
if (value.length() > 0) {
    return value.substring(0, value.length() - 1);
return value;
voidchomp(StringBuilder sb)
chomp
chopIfMatch(sb, '\n');
chopIfMatch(sb, '\r');
StringchompAllAndTrim(String str)

Remove the end of line and tab characters from a String.

StringBuffer buffer = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) != '\n' && str.charAt(i) != '\r' && str.charAt(i) != '\t')
        buffer.append(str.charAt(i));
str = buffer.toString();
buffer = null;
return str;
...
StringchompClosureOpen(String expr)
chomp Closure Open
if (!expr.endsWith("{")) {
    return expr;
int closurePos = expr.lastIndexOf("->");
if (closurePos < 0) {
    return expr;
return expr.substring(0, closurePos).trim();
...