Skip to content

StringUtils.chomp

Signature: StringUtils.chomp(inputString, separator*)

Returns: String result - the chomped string, or null if inputString is null

Removes separator from the end of inputString if it's there, otherwise leave it alone, handling null by returning null. If separator is not specified, a newline (\n, \r, or \r\n) is removed if it's there.

Parameters

Type Name Description Default
String inputString the String to be chomped, may be null
String separator* (optional) the separator to be removed from the end of the String, or newline if it is not specified '\n'

Example

// Example: remove a specific suffix from the end of a string.

StringUtils.chomp(
    "myFile.txt", // inputString
    ".txt"        // separator to remove
);                // "myFile"

// Example: remove a trailing newline (default behavior when separator is omitted).

StringUtils.chomp(
    "This is the end of the line.\\r\\n" // inputString
);                                       // "This is the end of the line."