Java String Chop chop(String str)

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

Description

chop

License

Apache License

Parameter

Parameter Description
str the string we are removing the last char from

Return

str with the last char removed. if str is null or empty, chop(String) has no effect.

Declaration

public static String chop(String str) 

Method Source Code

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

public class Main {
    /**/*from w  w  w  .j av a  2s. c  o  m*/
     * @param str the string we are removing the last char from
     * @return <code>str</code> with the last char removed. if <code>str</code> is <code>null</code> or empty, chop(String) has no effect.
     */
    public static String chop(String str) {
        return (str != null && str.length() > 0) ? str.substring(0, str.length() - 1) : str;
    }

    /**
     * @param str the string to shorten by one char
     * @param expectedEnd the expected end of <code>str</code>
     * @return str with the last char chopped off if <code>str</code> ends with <code>expectedEnd</code>, <code>str</code> itself otherwise
     */
    public static String chop(String str, char expectedEnd) {
        return (str != null && str.length() > 0 && str.endsWith(String.valueOf(expectedEnd)))
                ? str.substring(0, str.length() - 1)
                : str;
    }
}

Related

  1. chop(String s, int maxLength)
  2. chop(String src, String delim)
  3. chop(String str)
  4. chop(String str)
  5. chop(String str)
  6. chop(String str)
  7. chop(String str)
  8. chop(String str)
  9. chop(String str, int maxLength)