apache common Lang包StringUtils系列(七)

来源:互联网 发布:学校屏蔽网络如何破解 编辑:程序博客网 时间:2024/05/16 19:00
121 public static String replaceIgnoreCase(String text, String searchString, String replacement)
功能:同上,不区分大小写。
实例:
 StringUtils.replaceIgnoreCase(null, *, *)        = null
 StringUtils.replaceIgnoreCase("", *, *)          = ""
 StringUtils.replaceIgnoreCase("any", null, *)    = "any"
 StringUtils.replaceIgnoreCase("any", *, null)    = "any"
 StringUtils.replaceIgnoreCase("any", "", *)      = "any"
 StringUtils.replaceIgnoreCase("aba", "a", null)  = "aba"
 StringUtils.replaceIgnoreCase("abA", "A", "")    = "b"
 StringUtils.replaceIgnoreCase("aba", "A", "z")   = "zbz"
 
122 public static String replace(String text, String searchString, String replacement, int max)
功能:用字符串replacement替换掉出现在字符串text中的前max个searchString字符串。
参数:
 text-原字符串
 searchString-被替换的子串
 replacement-用于替换的字符串
 max-被替换的最大字符串个数,如果为-1则没有最大个数限制。
实例:
 StringUtils.replace(null, *, *, *)         = null
 StringUtils.replace("", *, *, *)           = ""
 StringUtils.replace("any", null, *, *)     = "any"
 StringUtils.replace("any", *, null, *)     = "any"
 StringUtils.replace("any", "", *, *)       = "any"
 StringUtils.replace("any", *, *, 0)        = "any"
 StringUtils.replace("abaa", "a", null, -1) = "abaa"
 StringUtils.replace("abaa", "a", "", -1)   = "b"
 StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
 StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
 StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
 StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
 
123 public static String replaceIgnoreCase(String text, String searchString, String replacement, int max)
功能:同上,不区分大小写。
实例:
 StringUtils.replaceIgnoreCase(null, *, *, *)         = null
 StringUtils.replaceIgnoreCase("", *, *, *)           = ""
 StringUtils.replaceIgnoreCase("any", null, *, *)     = "any"
 StringUtils.replaceIgnoreCase("any", *, null, *)     = "any"
 StringUtils.replaceIgnoreCase("any", "", *, *)       = "any"
 StringUtils.replaceIgnoreCase("any", *, *, 0)        = "any"
 StringUtils.replaceIgnoreCase("abaa", "a", null, -1) = "abaa"
 StringUtils.replaceIgnoreCase("abaa", "a", "", -1)   = "b"
 StringUtils.replaceIgnoreCase("abaa", "a", "z", 0)   = "abaa"
 StringUtils.replaceIgnoreCase("abaa", "A", "z", 1)   = "zbaa"
 StringUtils.replaceIgnoreCase("abAa", "a", "z", 2)   = "zbza"
 StringUtils.replaceIgnoreCase("abAa", "a", "z", -1)  = "zbzz"
 
124 public static String replaceEach(String text, String[] searchList, String[] replacementList)
功能:用replacementList数组中的字符串一一替换字符串text中的searchList数组中的字符串。
说明:
(1)replacementList只能替换searchList数组中相同下标的字符串;
(2)输入参数任何为null,则不执行替换操作,返回原text字符串;
(3)如果searchList数组和replacementList数组的长度不一致,则抛出IllegalArgumentException异常。
实例:  
  StringUtils.replaceEach(null, *, *)        = null
  StringUtils.replaceEach("", *, *)          = ""
  StringUtils.replaceEach("aba", null, null) = "aba"
  StringUtils.replaceEach("aba", new String[0], null) = "aba"
  StringUtils.replaceEach("aba", null, new String[0]) = "aba"
  StringUtils.replaceEach("aba", new String[]{"a"}, null)  = "aba"
  StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{""})  = "b"
  StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"})  = "aba"
  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"})  = "wcte"
  StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"})  = "dcte"
 
125 public static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)
功能:用replacementList数组循环替换text中的searchList数组字符串。
说明:
(1)replacementList只能替换searchList数组中相同下标的字符串;
(2)输入参数任何为null,则不执行替换操作,返回原text字符串;
(3)如果searchList数组和replacementList数组的长度不一致,则抛出IllegalArgumentException异常;
(4)替换过程中,如果存在死循环,则抛出IllegalStateException异常;
实例:
  StringUtils.replaceEachRepeatedly(null, *, *) = null
  StringUtils.replaceEachRepeatedly("", *, *) = ""
  StringUtils.replaceEachRepeatedly("aba", null, null) = "aba"
  StringUtils.replaceEachRepeatedly("aba", new String[0], null) = "aba"
  StringUtils.replaceEachRepeatedly("aba", null, new String[0]) = "aba"
  StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, null) = "aba"
  StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, new String[]{""}) = "b"
  StringUtils.replaceEachRepeatedly("aba", new String[]{null}, new String[]{"a"}) = "aba"
  StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
  StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "tcte"
  StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}) = IllegalStateException
  
126 public static String replaceChars(String str, char searchChar, char replaceChar)
功能:用字符replaceChar替换str字符串中的字符searchChar。
说明:
(1)str为null则返回null;
(2)str为“”则返回“”;
实例:
 StringUtils.replaceChars(null, *, *)        = null
 StringUtils.replaceChars("", *, *)          = ""
 StringUtils.replaceChars("abcba", 'b', 'y') = "aycya"
 StringUtils.replaceChars("abcba", 'z', 'y') = "abcba"
 
127 public static String replaceChars(String str, String searchChars, String replaceChars)
功能:用字符集replaceChars替换str字符串中的字符集searchChars。
说明:
(1)str为null则返回null;
(2)str为“”则返回“”;
(3)searchChars为null或“”,则返回字符串str;
(4)如果searchChars比replaceChars长,则searchCharsd多余部分被删除;
(5)如果replaceChars比searchCharsd长,则replaceChars多余部分被删除;
实例:
 StringUtils.replaceChars(null, *, *)           = null
 StringUtils.replaceChars("", *, *)             = ""
 StringUtils.replaceChars("abc", null, *)       = "abc"
 StringUtils.replaceChars("abc", "", *)         = "abc"
 StringUtils.replaceChars("abc", "b", null)     = "ac"
 StringUtils.replaceChars("abc", "b", "")       = "ac"
 StringUtils.replaceChars("abcba", "bc", "yz")  = "ayzya"
 StringUtils.replaceChars("abcba", "bc", "y")   = "ayya"
 StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
 StringUtils.replaceChars("hello", "ho", "jy") = "jelly"

128 public static String overlay(String str, String overlay, int start, int end)
功能:用字符串overlay覆盖str中从start开始到end前一字符结束的子串。
说明:
(1)str输入为null,则返回null;
(2)start或end为负数则视为0;
(3)start或end的值大于str的长度,则视为str的长度值;
(4)start的值总是start和end二者较小的值。
实例:
 StringUtils.overlay(null, *, *, *)            = null
 StringUtils.overlay("", "abc", 0, 0)          = "abc"
 StringUtils.overlay("abcdef", null, 2, 4)     = "abef"
 StringUtils.overlay("abcdef", "", 2, 4)       = "abef"
 StringUtils.overlay("abcdef", "", 4, 2)       = "abef"
 StringUtils.overlay("abcdef", "zzzz", 2, 4)   = "abzzzzef"
 StringUtils.overlay("abcdef", "zzzz", 4, 2)   = "abzzzzef"
 StringUtils.overlay("abcdef", "zzzz", -1, 4)  = "zzzzef"
 StringUtils.overlay("abcdef", "zzzz", 2, 8)   = "abzzzz"
 StringUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef"
 StringUtils.overlay("abcdef", "zzzz", 8, 10)  = "abcdefzzzz"
 
129 public static String chomp(String str)
功能:去掉字符串str末尾的一个换行符。
说明:
(1)如果str末尾没有换行符,则返回原字符串;
(2)一个换行符包括 "\n", "\r", or "\r\n";
实例:
StringUtils.chomp(null)          = null
 StringUtils.chomp("")            = ""
 StringUtils.chomp("abc \r")      = "abc "
 StringUtils.chomp("abc\n")       = "abc"
 StringUtils.chomp("abc\r\n")     = "abc"
 StringUtils.chomp("abc\r\n\r\n") = "abc\r\n"
 StringUtils.chomp("abc\n\r")     = "abc\n"
 StringUtils.chomp("abc\n\rabc")  = "abc\n\rabc"
 StringUtils.chomp("\r")          = ""
 StringUtils.chomp("\n")          = ""
 StringUtils.chomp("\r\n")        = ""

130 public static String chomp(String str,String separator)
功能:移除str末尾的separator字符串。
说明:
该函数在Lang4.0中被删除了,用removeEnd函数替代。
实例:
 StringUtils.chomp(null, *)         = null
 StringUtils.chomp("", *)           = ""
 StringUtils.chomp("foobar", "bar") = "foo"
 StringUtils.chomp("foobar", "baz") = "foobar"
 StringUtils.chomp("foo", "foo")    = ""
 StringUtils.chomp("foo ", "foo")   = "foo "
 StringUtils.chomp(" foo", "foo")   = " "
 StringUtils.chomp("foo", "foooo")  = "foo"
 StringUtils.chomp("foo", "")       = "foo"
 StringUtils.chomp("foo", null)     = "foo"
 
131 public static String chop(String str)
功能:移除字符串str末尾的最后一个字符。
说明:如果str是以\r\n结尾的,则同时移除\r\n
实例:
 StringUtils.chop(null)          = null
 StringUtils.chop("")            = ""
 StringUtils.chop("abc \r")      = "abc "
 StringUtils.chop("abc\n")       = "abc"
 StringUtils.chop("abc\r\n")     = "abc"
 StringUtils.chop("abc")         = "ab"
 StringUtils.chop("abc\nabc")    = "abc\nab"
 StringUtils.chop("a")           = ""
 StringUtils.chop("\r")          = ""
 StringUtils.chop("\n")          = ""
 StringUtils.chop("\r\n")        = ""

132 public static String repeat(String str, int repeat)
功能:重复str字符串repeat次后,形成一个新的字符串。
说明:
(1)如果str为null,则返回null;
(2)如果repeat是负数,则视为0;
实例:
 StringUtils.repeat(null, 2) = null
 StringUtils.repeat("", 0)   = ""
 StringUtils.repeat("", 2)   = ""
 StringUtils.repeat("a", 3)  = "aaa"
 StringUtils.repeat("ab", 2) = "abab"
 StringUtils.repeat("a", -2) = ""
 
133 public static String repeat(String str, String separator, int repeat)
功能:重复str字符串repeat次后,形成一个新的字符串,并且每次重复都添加一次字符串separator。
说明:
(1)如果str为null,则返回null;
(2)如果repeat是负数,则视为0;
实例:
 StringUtils.repeat(null, null, 2) = null
 StringUtils.repeat(null, "x", 2)  = null
 StringUtils.repeat("", null, 0)   = ""
 StringUtils.repeat("", "", 2)     = ""
 StringUtils.repeat("", "x", 3)    = "xxx"
 StringUtils.repeat("?", ", ", 3)  = "?, ?, ?"
 
134 public static String repeat(char ch, int repeat)
功能:返回指定字符ch重复repeat次后形成的字符串。
参数:
ch-用于重复的字符;
repeat-重复字符ch的次数,如果为负数则视为0.
实例:
 StringUtils.repeat('e', 0)  = ""
 StringUtils.repeat('e', 3)  = "eee"
 StringUtils.repeat('e', -2) = ""
 
135 public static String rightPad(String str, nt size)
功能:用空格字符右填充字符串str,使其长度达到size。
返回值:
(1)返回填充后的字符串;
(2)如果不需要填充,则返回原字符串;
(3)如果str为null,则返回null
实例:
 StringUtils.rightPad(null, *)   = null
 StringUtils.rightPad("", 3)     = "   "
 StringUtils.rightPad("bat", 3)  = "bat"
 StringUtils.rightPad("bat", 5)  = "bat  "
 StringUtils.rightPad("bat", 1)  = "bat"
 StringUtils.rightPad("bat", -1) = "bat"

136 public static String rightPad(String str, int size, char padChar)
功能:用字符padChar右填充字符串str,使其长度达到size。
返回值:
(1)返回填充后的字符串;
(2)如果不需要填充,则返回原字符串;
(3)如果str为null,则返回null
实例:
 StringUtils.rightPad(null, *, *)     = null
 StringUtils.rightPad("", 3, 'z')     = "zzz"
 StringUtils.rightPad("bat", 3, 'z')  = "bat"
 StringUtils.rightPad("bat", 5, 'z')  = "batzz"
 StringUtils.rightPad("bat", 1, 'z')  = "bat"
 StringUtils.rightPad("bat", -1, 'z') = "bat"
 
137 public static String rightPad(String str, int size, String padStr)
功能:用字符串padStr右填充字符串str,使其长度达到size。
参数:
str-源字符串;
size-填充后的长度;
padStr-用于填充的字符串,null或“”都被视为空格;
实例:
 StringUtils.rightPad(null, *, *)      = null
 StringUtils.rightPad("", 3, "z")      = "zzz"
 StringUtils.rightPad("bat", 3, "yz")  = "bat"
 StringUtils.rightPad("bat", 5, "yz")  = "batyz"
 StringUtils.rightPad("bat", 8, "yz")  = "batyzyzy"
 StringUtils.rightPad("bat", 1, "yz")  = "bat"
 StringUtils.rightPad("bat", -1, "yz") = "bat"
 StringUtils.rightPad("bat", 5, null)  = "bat  "
 StringUtils.rightPad("bat", 5, "")    = "bat  "
 
138 public static String leftPad(String str, int size)
功能:用空格字符左填充字符串str,使其长度达到size。
返回值:
(1)返回填充后的字符串;
(2)如果不需要填充,则返回原字符串;
(3)如果str为null,则返回null
实例:
 StringUtils.leftPad(null, *)   = null
 StringUtils.leftPad("", 3)     = "   "
 StringUtils.leftPad("bat", 3)  = "bat"
 StringUtils.leftPad("bat", 5)  = "  bat"
 StringUtils.leftPad("bat", 1)  = "bat"
 StringUtils.leftPad("bat", -1) = "bat"
 
139 public static String leftPad(String str, int size, char padChar)
功能:用字符padChar左填充字符串str,使其长度达到size。
实例:
 StringUtils.leftPad(null, *, *)     = null
 StringUtils.leftPad("", 3, 'z')     = "zzz"
 StringUtils.leftPad("bat", 3, 'z')  = "bat"
 StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
 StringUtils.leftPad("bat", 1, 'z')  = "bat"
 StringUtils.leftPad("bat", -1, 'z') = "bat"
 
140 public static String leftPad(String str, int size, String padStr)
功能:用字符串padStr左填充字符串str,使其长度达到size。
参数:
str-源字符串;
size-填充后的长度;
padStr-用于填充的字符串,null或“”都被视为空格;
实例: 
 StringUtils.leftPad(null, *, *)      = null
 StringUtils.leftPad("", 3, "z")      = "zzz"
 StringUtils.leftPad("bat", 3, "yz")  = "bat"
 StringUtils.leftPad("bat", 5, "yz")  = "yzbat"
 StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat"
 StringUtils.leftPad("bat", 1, "yz")  = "bat"
 StringUtils.leftPad("bat", -1, "yz") = "bat"
 StringUtils.leftPad("bat", 5, null)  = "  bat"
 StringUtils.leftPad("bat", 5, "")    = "  bat"
0 0