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)www.3ppt.com || 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;
}
}
分享到:
相关推荐
java字符转换类代码,可以实现判断字符串是否为空,并删除首尾空格,字符串替换函数,代码转换,GBK转换为ISO-8859-1,代码转换 从srcCode转换为destCode,代码转换,GBK转换为big5,替换非法字符,标记本身等于分隔...
java字符串操作大全,适合初学者,浅显易懂 部JAVA字符串操作 2008-07-11 15:39:42| 分类: JAVA | 标签: |字号大中小 订阅 . JAVA字符串的方法 String a = "53c015"; //Integer.parseInt(s, radix) radix设置为...
感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用。 函数: 1、从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:...
该子字符串始于指定索引处的字符,一直到此字符串末尾。 例如: "unhappy".substring(2) returns "happy" "Harbison".substring(3) returns "bison" "emptiness".substring(9) returns "" (an empty string) 参数:...
字符串 1、获取字符串的长度 length() 2 、判断字符串的前缀或后缀与已知字符串是否相同 前缀 startsWith(String s) 后缀 endsWith(String s) 3、比较两个字符串 equals(String s) 4、把字符串转化为相应的...
substring():返回指定索引范围内的子字符串。 toLowerCase():将字符串转换为小写。 toUpperCase():将字符串转换为大写。 字符串类在Java中广泛应用于各种场景,如文本处理、字符串匹配和格式化等。它是一种非常...
1.substring 方法 定义和用法 substring 方法用于提取字符串中介于两个指定下标之间的字符。 语法 stringObject.substring(start,stop) 参数 描述 start 必需。一个非负的整数,规定要提取的子串的第一个字符在 ...
本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
${fn:substring(你要截取的字符串),beginIndex,endIndex} 下面是JSTL中自带的方法列表以及其描述 函数名 函数说明 使用举例 fn:contains 判断字符串是否包含另外一个字符串 ${fn:contains(name,> fn:...
5. 字符串截取函数:substr,substring 24 6. 字符串截取函数:substr,substring 24 7. 字符串转大写函数:upper,ucase 24 8. 字符串转小写函数:lower,lcase 25 9. 去空格函数:trim 25 10. 左边去空格函数:ltrim ...
exec 方法 在指定字符串中执行一个匹配查找。 exp 方法 返回 e (自然对数的底) 的幂。 FileSystemObject 对象 提供对计算机文件系统的访问。 fixed 方法 将 HTML 的<TT> 标识添加到String 对象中的文本两端。 ...
‘Cts中包含Text字符串’); } indexOf用法: 返回 String 对象内第一次出现子字符串的字符位置。 strObj.indexOf(subString[, startIndex]) 参数 strObj 必选项。String 对象或文字。 subString 必选项
2.运行一个已经编译的程序时,Java解释器总是从指定类的main方法中的代码开始执行,因此,执行代码中必须有一个main函数。 3.Java是典型的强类型语言,即必须声明变量的类型,Java中有8种类型,6种数值类型(4个...
substr函数和substring函数都是用来从某个“母字符串”中提取“子字符串”的函数。但用法有些差别,下面分别介绍substr 方法 定义和用法 substr 方法用于返回一个从指定位置开始的指定长度的子字符串。 语法 string...
代码如下: // 去掉字符串的头空格(左空格) function LTrim(str){ var i; for(i=0;i(str.charAt(i)!=” xss=removed for(i=str.length-1;i>=0;i–){ if(str.charAt(i)!=” “) break; } str = str.substring(0,i+1)...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 ...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 ...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 实例45 请求的...
2.运行一个已经编译的程序时,Java解释器总是从指定类的main方法中的代码开始执行,因此,执行代码中必须有一个main函数。 3.Java是典型的强类型语言,即必须声明变量的类型,Java中有8种类型,6种数值类型(4个...
//以上为三种将字符串转换成整形的方法。 for(int i=0;i;i++) { StringBuffer sb=new StringBuffer(); //使用stringbuffer,是因为它是可追加的。 for(int j=0;j;j++) { sb.append('*'); } System.out.println(sb....