Android Utililty Methods String Trim

List of utility methods to do String Trim

Description

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

Method

Stringrtrim(final String text, String trimText)
rtrim
if (text == null) {
    return null;
if (trimText == null) {
    trimText = " ";
int pos = text.length() - 1;
for (; pos >= 0; pos--) {
...
StringtrimSuffix(final String text, final String suffix)
trim Suffix
if (text == null) {
    return null;
if (suffix == null) {
    return text;
if (text.endsWith(suffix)) {
    return text.substring(0, text.length() - suffix.length());
...
StringtrimPrefix(final String text, final String prefix)
trim Prefix
if (text == null) {
    return null;
if (prefix == null) {
    return text;
if (text.startsWith(prefix)) {
    return text.substring(prefix.length());
...
StringtrimSpace(String oldString)
Method trim space
if (null == oldString)
    return null;
if (0 == oldString.length())
    return "";
StringBuffer sbuf = new StringBuffer();
int oldLen = oldString.length();
for (int i = 0; i < oldLen; i++) {
    if (' ' != oldString.charAt(i))
...
intrightTrimSize(String s)
Returns the size of substring that does not contain any trailing spaces
int i = s.length();
while (i > 0) {
    i--;
    if (s.charAt(i) != ' ') {
        return i + 1;
return 0;
...
Stringtrim(String s, char c)
Removes characters 'c' from the beginning and the end of the string
int start = 0;
int end = s.length() - 1;
while (s.charAt(start) == c) {
    if (++start >= end) {
        return "";
while (s.charAt(end) == c) {
...
Stringtrim(String trimStr, String trimChars)
trim
int spIdx = findFirstNotOf(trimStr, trimChars);
if (spIdx < 0) {
    String buf = trimStr;
    return buf;
String trimStr2 = trimStr.substring(spIdx, trimStr.length());
spIdx = findLastNotOf(trimStr2, trimChars);
if (spIdx < 0) {
...
StringtrimEnd(String s, String extraChars)
Trim characters from only the end of a string.
int trimCount = 0;
while (trimCount < s.length()) {
    char ch = s.charAt(s.length() - trimCount - 1);
    if (Character.isWhitespace(ch)
            || (extraChars != null && extraChars.indexOf(ch) >= 0)) {
        trimCount++;
    } else {
        break;
...
StringtrimStart(String s, String extraChars)
Trim characters from only the beginning of a string.
int trimCount = 0;
while (trimCount < s.length()) {
    char ch = s.charAt(trimCount);
    if (Character.isWhitespace(ch)
            || (extraChars != null && extraChars.indexOf(ch) >= 0)) {
        trimCount++;
    } else {
        break;
...
StringbetterTrim(String input)
better Trim
input = input.trim();
if (input.length() == 0) {
    return input;
boolean didChange;
do {
    String oldInput = input;
    char firstChar = input.charAt(0);
...