java substring()函數刪除指定字串

來源:互聯網
上載者:User

java substring()函數刪除指定字串
public class main {

  /**
   * case insensitive removal of a substring if it is at the end of a source string,
   * otherwise returns the source string.
   *
   * 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.
   *
   * <pre>
   * stringutils.removeend(null, *)      = null
   * stringutils.removeend("", *)        = ""
   * stringutils.removeend(*, null)      = *
   * stringutils.removeend("www.domain.com", ".com.")  = "www.domain.com."
   * stringutils.removeend("www.domain.com", ".com")   = "www.domain"
   * stringutils.removeend("www.domain.com", "domain") = "www.domain.com"
   * stringutils.removeend("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 removeendignorecase(string str, string remove) {
      if (isempty(str) || isempty(remove)) {
          return str;
      }
      if (endswithignorecase(str, remove)) {
          return str.substring(0, str.length() - remove.length());
      }
      return str;
  }
  /**
   * case insensitive check if a string ends with a specified suffix.
   *
   * <code>null</code>s are handled without exceptions. two <code>null</code>
   * references are considered to be equal. the comparison is case insensitive.
   *
   * <pre>
   * stringutils.endswithignorecase(null, null)      = true
   * stringutils.endswithignorecase(null, "abcdef")  = false
   * stringutils.endswithignorecase("def", null)     = false
   * stringutils.endswithignorecase("def", "abcdef") = true
   * stringutils.endswithignorecase("def", "abcdef") = false
   * </pre>
   *
   * @see java.lang.string#endswith(string)
   * @param str  the string to check, may be null
   * @param suffix the suffix to find, may be null
   * @return <code>true</code> if the string ends with the suffix, case insensitive, or
   *  both <code>null</code>
   * @since 2.4
   */
  public static boolean endswithignorecase(string str, string suffix) {
      return endswith(str, suffix, true);
  }

  /**
   * check if a string ends with a specified suffix (optionally case insensitive).
   *
   * @see java.lang.string#endswith(string)
   * @param str  the string to check, may be null
   * @param suffix the suffix to find, may be null
   * @param ignorecase inidicates 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 endswith(string str, string suffix, boolean ignorecase) {
      if (str == null || suffix == null) {
          return (str == null && suffix == null);
      }
      if (suffix.length() > str.length()) {
          return false;
      }
      int stroffset = str.length() - suffix.length();
      return str.regionmatches(ignorecase, stroffset, suffix, 0, suffix.length());
  }
  // empty checks
  //-----------------------------------------------------------------------
  /**
   * checks if a string is empty ("") or null.
   *
   * <pre>
   * stringutils.isempty(null)      = true
   * stringutils.isempty("")        = true
   * stringutils.isempty(" ")       = false
   * stringutils.isempty("bob")     = false
   * stringutils.isempty("  bob  ") = false
   * </pre>
   *
   * note: this method changed in lang version 2.0.
   * it no longer trims the string.
   * that functionality is available in isblank().
   *
   * @param str  the string to check, may be null
   * @return <code>true</code> if the string is empty or null
   */
  public static boolean isempty(string str) {
      return str == null || str.length() == 0;
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.