apache common Lang包StringUtils系列(六)

来源:互联网 发布:js实现答题卡功能 编辑:程序博客网 时间:2024/04/30 01:54
101 public static String join(Object[] array,String separator)
功能:将数组array中的所有元素拼接成一个独立的字符串。并将字符串separator拼接在元素中间。
说明:
(1)分隔字符串不能被添加到首部和尾部。
(2)一个null的separator等同于空字符串(“”);
(3)数组array中的null对象或空字符串代表空字符串;
实例:
 StringUtils.join(null, *)     = null
 StringUtils.join([], *)   = ""
 StringUtils.join([null], *)    = ""
 StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
 StringUtils.join(["a", "b", "c"], null)  = "abc"
 StringUtils.join(["a", "b", "c"], "")    = "abc"
 StringUtils.join([null, "", "a"], ',')   = ",,a"
 
102 public static String join(Object[] array, String separator, int startIndex, int endIndex)
功能:将数组array中的所有元素拼接成一个独立的字符串。并将字符串separator拼接在元素中间。
参数:
 array-需要拼接的数组;
 separator-用于拼接到字符串中的分隔符;
 startIndex-开始拼接的数组索引号;
 endIndex-结束拼接的数据索引号;
实例:
 StringUtils.join(null, *, *, *)                = null
 StringUtils.join([], *, *, *)                  = ""
 StringUtils.join([null], *, *, *)              = ""
 StringUtils.join(["a", "b", "c"], "--", 0, 3)  = "a--b--c"
 StringUtils.join(["a", "b", "c"], "--", 1, 3)  = "b--c"
 StringUtils.join(["a", "b", "c"], "--", 2, 3)  = "c"
 StringUtils.join(["a", "b", "c"], "--", 2, 2)  = ""
 StringUtils.join(["a", "b", "c"], null, 0, 3)  = "abc"
 StringUtils.join(["a", "b", "c"], "", 0, 3)    = "abc"
 StringUtils.join([null, "", "a"], ',', 0, 3)   = ",,a"
 
103 public static String joinWith(String separator, Object... objects)
功能:将可变参数中的元素拼接成一个独立的字符串。
说明:
(1)separator不会添加到字符串的首部或尾部。
(2)null元素视为空字符串“”
实例:
 StringUtils.joinWith(",", {"a", "b"})        = "a,b"
 StringUtils.joinWith(",", {"a", "b",""})     = "a,b,"
 StringUtils.joinWith(",", {"a", null, "b"})  = "a,,b"
 StringUtils.joinWith(null, {"a", "b"})       = "ab"
 
104 public static String deleteWhitespace(String str)
功能:删除字符串str中的所有空白符。
说明:
(1)注意空白符的定义。
实例:
 StringUtils.deleteWhitespace(null)         = null
 StringUtils.deleteWhitespace("")           = ""
 StringUtils.deleteWhitespace("abc")        = "abc"
 StringUtils.deleteWhitespace("   ab  c  ") = "abc"
 
105 public static String removeStart(String str, String remove)
功能:当字符串remove在字符串str开头时,则移除。否则返回原字符串。
说明:
(1)如果str为null,则返回null;
(2)如果str为“”,则返回“”;
(3)如果remove为null,则返回原字符串str;
实例:
 StringUtils.removeStart(null, *)      = null
 StringUtils.removeStart("", *)        = ""
 StringUtils.removeStart(*, null)      = *
 StringUtils.removeStart("www.domain.com", "www.")   = "domain.com"
 StringUtils.removeStart("domain.com", "www.")       = "domain.com"
 StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com"
 StringUtils.removeStart("abc", "")    = "abc"

106 public static String removeStartIgnoreCase(String str, String remove)
功能:当字符串remove在字符串str开头时,则移除。否则返回原字符串。不区分大小写。
说明:
(1)如果str为null,则返回null;
(2)如果str为“”,则返回“”;
(3)如果remove为null,则返回原字符串str;
实例:
 StringUtils.removeStartIgnoreCase(null, *)      = null
 StringUtils.removeStartIgnoreCase("", *)        = ""
 StringUtils.removeStartIgnoreCase(*, null)      = *
 StringUtils.removeStartIgnoreCase("www.domain.com", "www.")   = "domain.com"
 StringUtils.removeStartIgnoreCase("www.domain.com", "WWW.")   = "domain.com"
 StringUtils.removeStartIgnoreCase("domain.com", "www.")       = "domain.com"
 StringUtils.removeStartIgnoreCase("www.domain.com", "domain") = "www.domain.com"
 StringUtils.removeStartIgnoreCase("abc", "")    = "abc"
 
107 public static String removeEnd(String str, String remove)
功能:当字符串remove在字符串str末尾时,则移除。否则返回原字符串。
说明:
(1)如果str为null,则返回null;
(2)如果str为“”,则返回“”;
(3)如果remove为null,则返回原字符串str;
实例:
 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"
 
108 public static String removeEndIgnoreCase(String str, String remove)
功能:当字符串remove在字符串str末尾时,则移除。否则返回原字符串。不区分大小写。
说明:
(1)如果str为null,则返回null;
(2)如果str为“”,则返回“”;
(3)如果remove为null,则返回原字符串str;
实例:
 StringUtils.removeEndIgnoreCase(null, *)      = null
 StringUtils.removeEndIgnoreCase("", *)        = ""
 StringUtils.removeEndIgnoreCase(*, null)      = *
 StringUtils.removeEndIgnoreCase("www.domain.com", ".com.")  = "www.domain.com"
 StringUtils.removeEndIgnoreCase("www.domain.com", ".com")   = "www.domain"
 StringUtils.removeEndIgnoreCase("www.domain.com", "domain") = "www.domain.com"
 StringUtils.removeEndIgnoreCase("abc", "")    = "abc"
 StringUtils.removeEndIgnoreCase("www.domain.com", ".COM") = "www.domain")
 StringUtils.removeEndIgnoreCase("www.domain.COM", ".com") = "www.domain")

109 public static String remove(String str,String remove)
功能:移除str中所有的子串remove。
说明:
(1)如果str为null,则返回null;
(2)如果str为“”,则返回“”;
(3)如果remove为null或“”,则返回原字符串str;
实例:
 StringUtils.remove(null, *)        = null
 StringUtils.remove("", *)          = ""
 StringUtils.remove(*, null)        = *
 StringUtils.remove(*, "")          = *
 StringUtils.remove("queued", "ue") = "qd"
 StringUtils.remove("queued", "zz") = "queued"
 
110 public static String removeIgnoreCase(String str,String remove)
功能:移除str中所有的子串remove,不区分大小写。
说明:
(1)如果str为null,则返回null;
(2)如果str为“”,则返回“”;
(3)如果remove为null或“”,则返回原字符串str;
实例:
 StringUtils.removeIgnoreCase(null, *)        = null
 StringUtils.removeIgnoreCase("", *)          = ""
 StringUtils.removeIgnoreCase(*, null)        = *
 StringUtils.removeIgnoreCase(*, "")          = *
 StringUtils.removeIgnoreCase("queued", "ue") = "qd"
 StringUtils.removeIgnoreCase("queued", "zz") = "queued"
 StringUtils.removeIgnoreCase("quEUed", "UE") = "qd"
 StringUtils.removeIgnoreCase("queued", "zZ") = "queued"


111 public static String remove(String str, char remove)
功能:移除str中所有的字符remove。
说明:
(1)如果str为null,则返回null;
(2)如果str为“”,则返回“”;
实例:
 StringUtils.remove(null, *)       = null
 StringUtils.remove("", *)         = ""
 StringUtils.remove("queued", 'u') = "qeed"
 StringUtils.remove("queued", 'z') = "queued"
 
112 public static String removeAll(String text,String regex)
功能:移除字符串text中能匹配正则表达式的所有子串。
参数:
 text-源字符串
 regex-正则表达式
实例:
 StringUtils.removeAll(null, *)      = null
 StringUtils.removeAll("any", null)  = "any"
 StringUtils.removeAll("any", "")    = "any"
 StringUtils.removeAll("any", ".*")  = ""
 StringUtils.removeAll("any", ".+")  = ""
 StringUtils.removeAll("abc", ".?")  = ""
 StringUtils.removeAll("A<__>\n<__>B", "<.*>")      = "A\nB"
 StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>")  = "AB"
 StringUtils.removeAll("ABCabc123abc", "[a-z]")     = "ABC123"
 
113 public static String removeFirst(String text, String regex)
功能:移除字符串text中第一个能匹配上正则表达式的子串。
参数:
 text-原字符串
 regex-正则表达式
实例:
 StringUtils.removeFirst(null, *)      = null
 StringUtils.removeFirst("any", null)  = "any"
 StringUtils.removeFirst("any", "")    = "any"
 StringUtils.removeFirst("any", ".*")  = ""
 StringUtils.removeFirst("any", ".+")  = ""
 StringUtils.removeFirst("abc", ".?")  = "bc"
 StringUtils.removeFirst("A<__>\n<__>B", "<.*>")      = "A\n<__>B"
 StringUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>")  = "AB"
 StringUtils.removeFirst("ABCabc123", "[a-z]")        = "ABCbc123"
 StringUtils.removeFirst("ABCabc123abc", "[a-z]+")    = "ABC123abc"

114 public static String replaceOnce(String text, String searchString, String replacement)
功能:用替换字符串替换一个字符串中的一个子串。只替换一次。
参数:
 text-原字符串;
 searchString-被替换的字符串
 replacement-用于替换的字符串
实例:
 StringUtils.replaceOnce(null, *, *)        = null
 StringUtils.replaceOnce("", *, *)          = ""
 StringUtils.replaceOnce("any", null, *)    = "any"
 StringUtils.replaceOnce("any", *, null)    = "any"
 StringUtils.replaceOnce("any", "", *)      = "any"
 StringUtils.replaceOnce("aba", "a", null)  = "aba"
 StringUtils.replaceOnce("aba", "a", "")    = "ba"
 StringUtils.replaceOnce("aba", "a", "z")   = "zba"
 
115 public static String replaceOnceIgnoreCase(String text, String searchString, String replacement)
功能:用替换字符串替换一个字符串中的一个子串。只替换一次。。不区分大小写。
参数:
 text-原字符串;
 searchString-被替换的字符串
 replacement-用于替换的字符串
实例:
 StringUtils.replaceOnceIgnoreCase(null, *, *)        = null
 StringUtils.replaceOnceIgnoreCase("", *, *)          = ""
 StringUtils.replaceOnceIgnoreCase("any", null, *)    = "any"
 StringUtils.replaceOnceIgnoreCase("any", *, null)    = "any"
 StringUtils.replaceOnceIgnoreCase("any", "", *)      = "any"
 StringUtils.replaceOnceIgnoreCase("aba", "a", null)  = "aba"
 StringUtils.replaceOnceIgnoreCase("aba", "a", "")    = "ba"
 StringUtils.replaceOnceIgnoreCase("aba", "a", "z")   = "zba"
 StringUtils.replaceOnceIgnoreCase("FoOFoofoo", "foo", "") = "Foofoo"
 
116 public static String replacePattern(String source, String regex, String replacement)
功能:替换text字符串中可以匹配正则表达式的子串。
参数:
source-源字符串。
regex-正则表达式。
replacement-用于替换子串的字符串。
实例:
 StringUtils.replacePattern(null, *, *)       = null
 StringUtils.replacePattern("any", null, *)   = "any"
 StringUtils.replacePattern("any", *, null)   = "any"
 StringUtils.replacePattern("", "", "zzz")    = "zzz"
 StringUtils.replacePattern("", ".*", "zzz")  = "zzz"
 StringUtils.replacePattern("", ".+", "zzz")  = ""
 StringUtils.replacePattern("<__>\n<__>", "<.*>", "z")       = "z"
 StringUtils.replacePattern("ABCabc123", "[a-z]", "_")       = "ABC___123"
 StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_")  = "ABC_123"
 StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "")   = "ABC123"
 StringUtils.replacePattern("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2")  = "Lorem_ipsum_dolor_sit"

117 public static String removePattern(String source,String regex)
功能:移除可以匹配上正则表达式的的子串。
参数:
source-原字符串
regex-正则表达式
实例:
 StringUtils.removePattern(null, *)       = null
 StringUtils.removePattern("any", null)   = "any"
 StringUtils.removePattern("A<__>\n<__>B", "<.*>")  = "AB"

 StringUtils.removePattern("ABCabc123", "[a-z]")    = "ABC123"


118 public static String replaceAll(String text, String regex, String replacement)
功能:用字符串replacement替换text字符串中可以匹配正则表达式regex的子串。
参数:
 source-原字符串
 regex-正则表达式
实例:
 StringUtils.replaceAll(null, *, *)       = null
 StringUtils.replaceAll("any", null, *)   = "any"
 StringUtils.replaceAll("any", *, null)   = "any"
 StringUtils.replaceAll("", "", "zzz")    = "zzz"
 StringUtils.replaceAll("", ".*", "zzz")  = "zzz"
 StringUtils.replaceAll("", ".+", "zzz")  = ""
 StringUtils.replaceAll("abc", "", "ZZ")  = "ZZaZZbZZcZZ"
 StringUtils.replaceAll("<__>\n<__>", "<.*>", "z")      = "z\nz"
 StringUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z")  = "z"
 StringUtils.replaceAll("ABCabc123", "[a-z]", "_")       = "ABC___123"
 StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_")  = "ABC_123"
 StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "")   = "ABC123"
 StringUtils.replaceAll("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2")  = "Lorem_ipsum_dolor_sit"

119 public static String replaceFirst(String text, String regex, String replacement)
功能:用字符串replacement替换第一个能匹配上正则表达式regex的子串。
参数:
 text-原字符串
 regex-正则表达式
 replacement-用于替换的字符串
实例:
 StringUtils.replaceFirst(null, *, *)       = null
 StringUtils.replaceFirst("any", null, *)   = "any"
 StringUtils.replaceFirst("any", *, null)   = "any"
 StringUtils.replaceFirst("", "", "zzz")    = "zzz"
 StringUtils.replaceFirst("", ".*", "zzz")  = "zzz"
 StringUtils.replaceFirst("", ".+", "zzz")  = ""
 StringUtils.replaceFirst("abc", "", "ZZ")  = "ZZabc"
 StringUtils.replaceFirst("<__>\n<__>", "<.*>", "z")      = "z\n<__>"
 StringUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z")  = "z"
 StringUtils.replaceFirst("ABCabc123", "[a-z]", "_")          = "ABC_bc123"
 StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_")  = "ABC_123abc"
 StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "")   = "ABC123abc"
 StringUtils.replaceFirst("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2")  = "Lorem_ipsum  dolor   sit"
 
120 public static String replace(String text, String searchString, String replacement)
功能:用字符串replacement替换掉所有出现在字符串text中的searchString字符串。
参数:
 text-原字符串
 searchString-被替换的子串
 replacement-用于替换的字符串
实例:
 StringUtils.replace(null, *, *)        = null
 StringUtils.replace("", *, *)          = ""
 StringUtils.replace("any", null, *)    = "any"
 StringUtils.replace("any", *, null)    = "any"
 StringUtils.replace("any", "", *)      = "any"
 StringUtils.replace("aba", "a", null)  = "aba"
 StringUtils.replace("aba", "a", "")    = "b"
 StringUtils.replace("aba", "a", "z")   = "zbz"

0 0
原创粉丝点击