java删除指定字符串函数_java substring()函数删除指定字符串

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 null source string will return null.

* an empty ("") source string will return the empty string.

* a null search string will return the source string.

*

*

 
 

* 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"

*

*

* @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,

null 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.

*

* nulls are handled without exceptions. two null

* references are considered to be equal. the comparison is case insensitive.

*

*

 
 

* 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

*

*

* @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 true if the string ends with the suffix, case insensitive, or

*  both null

* @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 true if the string starts with the prefix or

*  both null

*/

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.

*

*

 
 

* stringutils.isempty(null)      = true

* stringutils.isempty("")        = true

* stringutils.isempty(" ")       = false

* stringutils.isempty("bob")     = false

* stringutils.isempty("  bob  ") = false

*

*

* 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 true if the string is empty or null

*/

public static boolean isempty(string str) {

return str == null || str.length() == 0;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值