Java String Chomp chomp(String str, String sep)

Here you can find the source of chomp(String str, String sep)

Description

Remove the last value of a supplied String, and everything after it from a String.

License

Open Source License

Parameter

Parameter Description
str String to chomp from
sep String to chomp

Exception

Parameter Description
NullPointerException if str or sep is <code>null</code>

Return

String without chomped ending

Declaration

public static String chomp(String str, String sep) 

Method Source Code

//package com.java2s;

public class Main {
    public static String chomp(String str) {
        return chomp(str, "\n");
    }//  w w  w .  j a  va  2 s  .c  o m

    /**
     * <p>
     * Remove the last value of a supplied String, and everything after it from
     * a String.
     * </p>
     * 
     * @param str
     *            String to chomp from
     * @param sep
     *            String to chomp
     * @return String without chomped ending
     * @throws NullPointerException
     *             if str or sep is <code>null</code>
     */
    public static String chomp(String str, String sep) {
        int idx = str.lastIndexOf(sep);
        if (idx != -1) {
            return str.substring(0, idx);
        } else {
            return str;
        }
    }
}

Related

  1. chomp(String str)
  2. chomp(String str)
  3. chomp(String str)
  4. chomp(String str)
  5. chomp(String str)
  6. chomp(String str, String separator)
  7. chomp(String value)
  8. chomp(StringBuilder sb)
  9. chompAllAndTrim(String str)