Java String Chomp chomp(final String s, final String suffix)

Here you can find the source of chomp(final String s, final String suffix)

Description

Remove the given suffix from the given string, if present.

License

Open Source License

Parameter

Parameter Description
s the string
suffix the suffix to remove, if present

Return

if the string ends with the given suffix, the string with the suffix removed. Otherwise, return the original string

Declaration

public static String chomp(final String s, final String suffix) 

Method Source Code

//package com.java2s;
/**//from  w  ww  .  ja  v a  2s .c o  m
 * swing-revival:
 * Swing Revival Toolkit
 *
 * Copyright (c) 2009 by Alistair A. Israel.
 *
 * This software is made available under the terms of the MIT License.
 * See LICENSE.txt.
 *
 * Created Sep 30, 2009
 */

public class Main {
    /**
     * Remove the given suffix from the given string, if present.
     *
     * @param s
     *        the string
     * @param suffix
     *        the suffix to remove, if present
     * @return if the string ends with the given suffix, the string with the suffix removed. Otherwise, return the
     *         original string
     */
    public static String chomp(final String s, final String suffix) {
        if (hasLength(s) && hasLength(suffix) && s.endsWith(suffix)) {
            return s.substring(0, s.length() - suffix.length());
        }
        return s;
    }

    /**
     * @param s
     *        a string
     * @return true if the given string is not null, and not 0-length
     */
    public static boolean hasLength(final String s) {
        return !isNullOrEmpty(s);
    }

    /**
     * @param s
     *        a string
     * @return true if the given string is null or 0-length (<code>""</code>)
     */
    public static boolean isNullOrEmpty(final String s) {
        return null == s || s.length() == 0;
    }
}

Related

  1. chomp(CharSequence prefix, CharSequence full)
  2. chomp(String in)
  3. chomp(String inStr)
  4. chomp(String options)
  5. chomp(String path)