Java String Trim Right rTrim(String str)

Here you can find the source of rTrim(String str)

Description

r Trim

License

Apache License

Declaration

public static String rTrim(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String rTrim(String str) {
        return trimAt(str.length(), str);
    }//from   w w w  .j av  a2  s. c  o  m

    public static String trimAt(int index, String str) {
        int rightPos, leftPos;
        for (rightPos = index; rightPos < str.length(); rightPos++) {
            char c = str.charAt(rightPos);
            if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
                continue;
            } else {
                break;
            }
        }

        for (leftPos = index - 1; leftPos > -1; leftPos--) {
            char c = str.charAt(leftPos);
            if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
                continue;
            } else {
                leftPos++;
                break;
            }
        }
        return str.substring(0, leftPos) + str.substring(rightPos);
    }
}

Related

  1. rtrim(String str)
  2. rtrim(String str)
  3. rtrim(String str)
  4. rtrim(String str)
  5. rtrim(String str)
  6. rtrim(String str)
  7. rTrim(String str)
  8. rtrim(String str, String charList)
  9. rtrim(String str, String defaultValue)