Java String Strip stripTrailingChar(String input, char c)

Here you can find the source of stripTrailingChar(String input, char c)

Description

Removes a single character from the end of a string if it matches.

License

Open Source License

Parameter

Parameter Description
input String to remove from, if null returns null
c character to match

Return

Original string minus matched character

Declaration

public static String stripTrailingChar(String input, char c) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    /**/* w  w w . j a v  a  2s . c  o m*/
     * Removes a single character from the end of a string if it matches.
     * @param input String to remove from, if null returns null
     * @param c character to match
     * @return Original string minus matched character
     */
    public static String stripTrailingChar(String input, char c) {
        if (input == null)
            return null;
        if (input.isEmpty())
            return input;

        char[] charArray = input.toCharArray();

        if (charArray[charArray.length - 1] == c) {
            return new String(
                    Arrays.copyOf(charArray, charArray.length - 1));
        } else {
            return input;
        }
    }
}

Related

  1. stripLeadingSlash(String str)
  2. stripNamespaceDeclarations(String xml)
  3. stripTags(final String tagged)
  4. stripTags(String html)
  5. stripTags(String in)
  6. stripUnsupportedChars(String str)