Java String Trim Right rtrim(String s, Character c)

Here you can find the source of rtrim(String s, Character c)

Description

rtrim

License

Open Source License

Declaration

public static String rtrim(String s, Character c) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   ww w  . ja v a  2 s .  com
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

public class Main {
    public static String rtrim(String s, Character c) {
        if (s == null) {
            return null;
        }
        if (c == null) {
            return s;
        }
        int len = s.length();
        int st = 0;
        char[] val = s.toCharArray();
        while ((st < len) && (val[len - 1] == c)) {
            len--;
        }
        return ((st > 0) || (len < s.length())) ? s.substring(st, len) : s;
    }

    public static int length(String s) {
        if (s == null) {
            return 0;
        } else {
            return s.length();
        }
    }

    public static String substring(String s, int start) {
        if (s == null || start >= s.length()) {
            return "";
        }
        return s.substring(start);
    }

    public static String substring(String s, int start, int len) {
        if (s == null || start >= s.length()) {
            return "";
        }
        len = Math.min(s.length() - start, len);
        return s.substring(start, start + len);
    }
}

Related

  1. rtrim(String s)
  2. rtrim(String s)
  3. rtrim(String s)
  4. rtrim(String s, char character)
  5. rtrim(String s, char character)
  6. rtrim(String source)
  7. rtrim(String source)
  8. rtrim(String src, char ch, int nLen)
  9. rtrim(String str)