Java String remove start ignoring case

Description

Java String remove start ignoring case


//package com.demo2s;

public class Main {
  public static void main(String[] argv) throws Exception {
    String str = "demo2s.com";
    String remove = "Demo";
    System.out.println(removeStartIgnoreCase(str, remove));
  }/*from  www .j  av a  2s  .c  om*/

  /**
   * The empty String <code>""</code>.
   * 
   * @since 2.0
   */
  public static final String EMPTY = "";

  /**
   * <p>
   * Case insensitive removal of a substring if it is at the begining of a source
   * string, otherwise returns the source string.
   * </p>
   *
   * <p>
   * A <code>null</code> source string will return <code>null</code>. An empty
   * ("") source string will return the empty string. A <code>null</code> search
   * string will return the source string.
   * </p>
   *
   * <pre>
   * removeStartIgnoreCase(null, *)      = null
   * removeStartIgnoreCase("", *)        = ""
   * removeStartIgnoreCase(*, null)      = *
   * removeStartIgnoreCase("www.domain.com", "www.")   = "domain.com"
   * removeStartIgnoreCase("www.domain.com", "WWW.")   = "domain.com"
   * removeStartIgnoreCase("domain.com", "www.")       = "domain.com"
   * removeStartIgnoreCase("www.domain.com", "domain") = "www.domain.com"
   * removeStartIgnoreCase("abc", "")    = "abc"
   * </pre>
   *
   * @param str
   *          the source String to search, may be null
   * @param remove
   *          the String to search for (case insensitive) and remove, may be null
   * @return the substring with the string removed if found, <code>null</code> if
   *         null String input
   * @since 2.4
   */
  public static String removeStartIgnoreCase(String str, String remove) {
    if (isEmpty(str) || isEmpty(remove)) {
      return str;
    }
    if (startsWithIgnoreCase(str, remove)) {
      return str.substring(remove.length());
    }
    return str;
  }
  public static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
  }

  /**
   * <p>
   * Case insensitive check if a String starts with a specified prefix.
   * </p>
   *
   * <p>
   * <code>null</code>s are handled without exceptions. Two <code>null</code>
   * references are considered to be equal. The comparison is case insensitive.
   * </p>
   *
   * <pre>
   * startsWithIgnoreCase(null, null)      = true
   * startsWithIgnoreCase(null, "abcdef")  = false
   * startsWithIgnoreCase("abc", null)     = false
   * startsWithIgnoreCase("abc", "abcdef") = true
   * startsWithIgnoreCase("abc", "ABCDEF") = true
   * </pre>
   *
   * @see java.lang.String#startsWith(String)
   * @param str
   *          the String to check, may be null
   * @param prefix
   *          the prefix to find, may be null
   * @return <code>true</code> if the String starts with the prefix, case
   *         insensitive, or both <code>null</code>
   * @since 2.4
   */
  public static boolean startsWithIgnoreCase(String str, String prefix) {
    return startsWith(str, prefix, true);
  }

  /**
   * <p>
   * Check if a String starts with a specified prefix (optionally case
   * insensitive).
   * </p>
   *
   * @see java.lang.String#startsWith(String)
   * @param str
   *          the String to check, may be null
   * @param prefix
   *          the prefix to find, may be null
   * @param ignoreCase
   *          indicates whether the compare should ignore case (case insensitive)
   *          or not.
   * @return <code>true</code> if the String starts with the prefix or both
   *         <code>null</code>
   */
  private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
    if (str == null || prefix == null) {
      return (str == null && prefix == null);
    }
    if (prefix.length() > str.length()) {
      return false;
    }
    return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
  }
}

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



PreviousNext

Related