StringUtils 工具类的常用方法
jopen
13年前
<p>StringUtils 源码,使用的是commons-lang3-3.1包。</p> <p>下载地址 http://commons.apache.org/lang/download_lang.cgi</p> <p><br /> 以下是StringUtils的各项用法<br /> <strong>1.空字符串检查</strong><br /> 使用函数: StringUtils.isBlank(testString)<br /> 函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False<br /> 例程:<br /> String test = "";<br /> String test2 = "\n\n\t";<br /> String test3 = null;<br /> String test4 = "Test";</p> <p> System.out.println( "test blank? " + StringUtils.isBlank( test ) );<br /> System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );<br /> System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );<br /> System.out.println( "test4 blank? " + StringUtils.isBlank( test4 ) );<br /> 输出如下:<br /> test blank? true<br /> test2 blank? true<br /> test3 blank? true<br /> test4 blank? False<br /> 函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.</p> <p><br /> <strong>2.清除空白字符<br /> </strong>使用函数: StringUtils.trimToNull(testString)<br /> 函数介绍:清除掉testString首尾的空白字符,如果仅testString全由空白字符<br /> (whitespace)组成则返回null<br /> 例程:<br /> String test1 = "\t";<br /> String test2 = " A Test ";<br /> String test3 = null;</p> <p> System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) );<br /> System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) );<br /> System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );</p> <p>输出如下:<br /> test1 trimToNull: null<br /> test2 trimToNull: A Test<br /> test3 trimToNull: null</p> <p>注意:函数StringUtils.trim(testString)与<br /> StringUtils.trimToNull(testString)功能类似,但testString由空白字符<br /> (whitespace)组成时返回零长度字符串。</p> <p><br /> <strong>3.取得字符串的缩写</strong><br /> 使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)<br /> 函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串.<br /> 例程:<br /> String test = "This is a test of the abbreviation.";<br /> String test2 = "Test";</p> <p> System.out.println( StringUtils.abbreviate( test, 15 ) );<br /> System.out.println( StringUtils.abbreviate( test, 5,15 ) );<br /> System.out.println( StringUtils.abbreviate( test2, 10 ) );<br /> 输出如下:<br /> This is a te...<br /> ...is a test...<br /> Test</p> <p><strong>4.劈分字符串</strong><br /> 使用函数: StringUtils.split(testString,splitChars,arrayLength)<br /> 函数介绍:splitChars中可以包含一系列的字符串来劈分testString,并可以设定得<br /> 到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下<br /> 不要设定长度.<br /> 例程:<br /> String input = "A b,c.d|e";<br /> String input2 = "Pharmacy, basketball funky";</p> <p> String[] array1 = StringUtils.split( input, " ,.|");<br /> String[] array2 = StringUtils.split( input2, " ,", 2 );</p> <p><br /> System.out.println( ArrayUtils.toString( array1 ) );<br /> System.out.println( ArrayUtils.toString( array2 ) );<br /> 输出如下:<br /> {A,b,c,d,e}<br /> {Pharmacy,basketball funky}</p> <p><strong>5.查找嵌套字符串</strong><br /> 使用函数:StringUtils.substringBetween(testString,header,tail)<br /> 函数介绍:在testString中取得header和tail之间的字符串。不存在则返回空<br /> 例程:<br /> String htmlContent = "ABC1234ABC4567";<br /> System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));<br /> System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));<br /> 输出如下:<br /> ABC<br /> null</p> <p><br /> <strong>6.去除尾部换行符</strong><br /> 使用函数:StringUtils.chomp(testString)<br /> 函数介绍:去除testString尾部的换行符<br /> 例程:<br /> String input = "Hello\n";<br /> System.out.println( StringUtils.chomp( input ));<br /> String input2 = "Another test\r\n";<br /> System.out.println( StringUtils.chomp( input2 ));<br /> 输出如下:<br /> Hello<br /> Another test</p> <p><br /> <strong>7.重复字符串</strong><br /> 使用函数:StringUtils.repeat(repeatString,count)<br /> 函数介绍:得到将repeatString重复count次后的字符串<br /> 例程:<br /> System.out.println( StringUtils.repeat( "*", 10));<br /> System.out.println( StringUtils.repeat( "China ", 5));<br /> 输出如下:<br /> **********<br /> China China China China China</p> <p>其他函数:StringUtils.center( testString, count,repeatString );<br /> 函数介绍:把testString插入将repeatString重复多次后的字符串中间,得到字符串<br /> 的总长为count<br /> 例程:<br /> System.out.println( StringUtils.center( "China", 11,"*"));<br /> 输出如下:<br /> ***China***</p> <p><br /> <strong>8.颠倒字符串</strong><br /> 使用函数:StringUtils.reverse(testString)<br /> 函数介绍:得到testString中字符颠倒后的字符串<br /> 例程:<br /> System.out.println( StringUtils.reverse("ABCDE"));<br /> 输出如下:<br /> EDCBA</p> <p><strong>9.判断字符串内容的类型<br /> </strong>函数介绍:<br /> StringUtils.isNumeric( testString ) :如果testString全由数字组成返回True<br /> StringUtils.isAlpha( testString ) :如果testString全由字母组成返回True<br /> StringUtils.isAlphanumeric( testString ) :如果testString全由数字或数字组<br /> 成返回True<br /> StringUtils.isAlphaspace( testString ) :如果testString全由字母或空格组<br /> 成返回True</p> <p>例程:<br /> String state = "Virginia";<br /> System.out.println( "Is state number? " + StringUtils.isNumeric(<br /> state ) );<br /> System.out.println( "Is state alpha? " + StringUtils.isAlpha( state )<br /> );<br /> System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );<br /> System.out.println( "Is state alphaspace? " + StringUtils.isAlphaSpace( state ) );<br /> 输出如下:<br /> Is state number? false<br /> Is state alpha? true<br /> Is state alphanumeric? true<br /> Is state alphaspace? true</p> <p><strong>10.取得某字符串在另一字符串中出现的次数<br /> </strong>使用函数:StringUtils.countMatches(testString,seqString)<br /> 函数介绍:取得seqString在testString中出现的次数,未发现则返回零<br /> 例程:<br /> System.out.println(StringUtils.countMatches( "Chinese People", "e"<br /> ));<br /> 输出:<br /> 4</p> <p><strong>11.部分截取字符串</strong><br /> 使用函数:<br /> StringUtils.substringBetween(testString,fromString,toString ):取得两字符<br /> 之间的字符串<br /> StringUtils.substringAfter( ):取得指定字符串后的字符串<br /> StringUtils.substringBefore( ):取得指定字符串之前的字符串<br /> StringUtils.substringBeforeLast( ):取得最后一个指定字符串之前的字符串<br /> StringUtils.substringAfterLast( ):取得最后一个指定字符串之后的字符串</p> <p>函数介绍:上面应该都讲明白了吧。<br /> 例程:<br /> String formatted = " 25 * (30,40) [50,60] | 30";<br /> System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );<br /> System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(", "," ) );<br /> System.out.print(", N2: " + StringUtils.substringBetween( formatted, ",", ")" ) );<br /> System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[", "," ) );<br /> System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",", "]" ) );<br /> System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) );<br /> 输出如下:<br /> N0: 25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5: 30<br /> <br /> </p> <p><strong>1. 检查字符串是否为空:</strong></p> <p> </p> <p style="padding-left:30px;"> static boolean isBlank(CharSequence str) 判断字符串是否为空或null;<br /> static boolean isNotBlank(CharSequence str) 判断字符串是否非空或非null;</p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"><span style="background-color:#c0c0c0;"><span style="background-color:#ffffff;"> StringUtils.isBlank("a");<br /> 返回结果为: false;</span></span></p> <p> </p> <p><strong>2. 缩进字符串:</strong></p> <p> </p> <p style="padding-left:30px;"> static String abbreviate(String str, int maxWidth) 缩进字符串,第二个参数至少为4(包括...)</p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"> StringUtils.abbreviate("abcdefg", 20);<br /> 返回结果为:abcdefg (正常显示)</p> <p style="padding-left:30px;"> StringUtils.abbreviate("abcdefg", 4);<br /> 返回结果为:a...</p> <p> </p> <p><strong>3. 首字母大写:</strong></p> <p> </p> <p style="padding-left:30px;"> static String capitalize(String str) 首字母大写<br /> static String uncapitalize(String str)首字母小写 </p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"> StringUtils.capitalize("abcdefg");<br /> 返回结果:Abcdefg</p> <p> </p> <p><strong>4. 字符串显示在一个大字符串的位置:</strong></p> <p> </p> <p style="padding-left:30px;"> static String center(String str, int size); 默认以空格填充<br /> static String center(String str, int size, String padString); 其余位置字符串填充<br /> public static String leftPad(String str,int size); 左侧空格填充<br /> public static String leftPad(String str,int size,String padStr);左侧字符串填充<br /> public static String rightPad(String str,int size); 左侧空格填充<br /> public static String rightPad(String str,int size,String padStr);左侧字符串填充<br /> </p> <p style="padding-left:30px;"> StringUtils.center("abcdefg", 20);<br /> 返回结果: abcdefg </p> <p style="padding-left:30px;"> StringUtils.center("abcdefg", 20,"*_");<br /> 返回结果:*_*_*_abcdefg*_*_*_*</p> <p style="padding-left:30px;"> StringUtils.leftPad("abc", 10, "*");<br /> 返回结果:*******abc</p> <p> </p> <p><strong>5. 重复字符串次数</strong></p> <p> </p> <p style="padding-left:30px;"> static String repeat(String str, int repeat); </p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"> StringUtils.repeat("abc", 5); <br /> 返回结果:abcabcabcabcabc</p> <p> </p> <p><strong>6. 是否全是大写,是否全是小写(3.0版本)</strong></p> <p> </p> <p style="padding-left:30px;"> public static boolean isAllLowerCase(String str);<br /> public static boolean isAllUpperCase(String str);</p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"> StringUtils.isAllLowerCase("abC");<br /> 返回结果:false</p> <p> </p> <p><strong>7. 是否都是由字母组成:</strong></p> <p> </p> <p style="padding-left:30px;"> public static boolean isAlpha(String str); 只由字母组成<br /> public static boolean isAlphaSpace(String str); 只有字母和空格组成<br /> public static boolean isAlphanumeric(String str);只由字母和数字组成<br /> public static boolean isAlphanumericSpace(String str);只由字母数字和空格组成<br /> public static boolean isNumeric(String str);只由数字组成<br /> public static boolean isNumericSpace(String str);只由数字和空格组成</p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"> StringUtils.isAlpha("a2bdefg");<br /> 返回结果:false</p> <p> </p> <p><strong>8. 小字符串在大字符串中的匹配次数</strong></p> <p> </p> <p style="padding-left:30px;">public static int countMatches(String str,String sub);</p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;">StringUtils.countMatches("ababsssababa", "ab");<br /> 返回结果:4</p> <p> </p> <p><strong>9. 字符串倒转</strong></p> <p> </p> <p style="padding-left:30px;"> public static String reverse(String str);</p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"> StringUtils.reverse("abcdef");<br /> 返回结果:fedcba</p> <p> </p> <p><strong>10. 大小写转换,空格不动<br /> </strong></p> <p style="padding-left:30px;"> public static String swapCase(String str);</p> <p style="padding-left:30px;"> </p> <p style="padding-left:30px;"> StringUtils.swapCase("I am a-A*a")<br /> 返回结果:i AM A-a*A</p> <div class="postTitle"> <a id="viewpost1_TitleUrl" class="postTitle2" href="/misc/goto?guid=4959500679629547669">StringUtils工具类的使用</a> </div> <p>一、数组转成字符串: <br /> 1、 将数组中的字符转换为一个字符串 <br /> 将数组中的字符转换为一个字符串 <br /> <br /> @param strToConv 要转换的字符串 ,默认以逗号分隔 <br /> @return 返回一个字符串 <br /> String[3] s={"a","b","c"} <br /> StringUtil.convString(s)="a,b,c" <br /> 2、 static public String converString(String strToConv) <br /> @param strToConv 要转换的字符串 , <br /> @param conv 分隔符,默认以逗号分隔 <br /> @return 同样返回一个字符串 <br /> <br /> String[3] s={"a","b","c"} <br /> StringUtil.convString(s,"@")="a@b@c" <br /> static public String converString(String strToConv, String conv) <br /> <br /> <br /> 二、空值检测: <br /> 3、 <br /> <br /> Checks if a String is empty ("") or null. <br /> <br /> <br /> 判断一个字符串是否为空,空格作非空处理。 StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false <br /> <br /> NOTE: This method changed in Lang version 2.0. <br /> <br /> It no longer trims the String. <br /> That functionality is available in isBlank(). <br /> <br /> <br /> @param str the String to check, may be null <br /> @return true if the String is empty or null <br /> public static boolean isEmpty(String str) <br /> <br /> <br /> 三、非空处理: <br /> 4、 <br /> Checks if a String is not empty ("") and not null. <br /> <br /> <br /> 判断一个字符串是否非空,空格作非空处理. StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true <br /> <br /> @param str the String to check, may be null <br /> @return true if the String is not empty and not null <br /> public static boolean isNotEmpty(String str) <br /> <br /> 5、 <br /> <br /> Checks if a String is not empty (""), not null and not whitespace only. <br /> <br /> <br /> 判断一个字符串是否非空,空格作空处理. StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true <br /> <br /> @param str the String to check, may be null <br /> @return true if the String is <br /> not empty and not null and not whitespace <br /> @since 2.0 <br /> public static boolean isNotBlank(String str) <br /> <br /> <br /> 四、 空格处理 <br /> 6、 <br /> Removes control characters (char <= 32) from both <br /> <br /> ends of this String, handling null by returning <br /> null. <br /> <br /> <br /> The String is trimmed using {@link String#trim()}. <br /> <br /> Trim removes start and end characters <= 32. <br /> To strip whitespace use {@link //strip(String)}. <br /> <br /> <br /> To trim your choice of characters, use the <br /> <br /> {@link //strip(String, String)} methods. <br /> <br /> <br /> 格式化一个字符串中的空格,有非空判断处理; StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc" <br /> <br /> @param str the String to be trimmed, may be null <br /> @return the trimmed string, null if null String input <br /> public static String trim(String str) <br /> <br /> 7、 <br /> <br /> <br /> Removes control characters (char <= 32) from both <br /> <br /> ends of this String returning null if the String is <br /> empty ("") after the trim or if it is null. <br /> <br /> The String is trimmed using {@link String#trim()}. <br /> <br /> Trim removes start and end characters <= 32. <br /> To strip whitespace use {@link /stripToNull(String)}. <br /> <br /> <br /> 格式化一个字符串中的空格,有非空判断处理,如果为空返回null; StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc" <br /> <br /> @param str the String to be trimmed, may be null <br /> @return the trimmed String, <br /> null if only chars <= 32, empty or null String input <br /> @since 2.0 <br /> public static String trimToNull(String str) <br /> <br /> 8、 <br /> <br /> <br /> Removes control characters (char <= 32) from both <br /> <br /> ends of this String returning an empty String ("") if the String <br /> is empty ("") after the trim or if it is null. <br /> <br /> The String is trimmed using {@link String#trim()}. <br /> <br /> Trim removes start and end characters <= 32. <br /> To strip whitespace use {@link /stripToEmpty(String)}. <br /> <br /> <br /> 格式化一个字符串中的空格,有非空判断处理,如果为空返回""; StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc" <br /> <br /> @param str the String to be trimmed, may be null <br /> @return the trimmed String, or an empty String if null input <br /> @since 2.0 <br /> public static String trimToEmpty(String str) <br /> <br /> <br /> 五、 字符串比较: <br /> 9、 <br /> Compares two Strings, returning true if they are equal. <br /> <br /> <br /> nulls are handled without exceptions. Two null <br /> <br /> references are considered to be equal. The comparison is case sensitive. <br /> <br /> <br /> 判断两个字符串是否相等,有非空处理。 StringUtils.equals(null, null) = true StringUtils.equals(null, "abc") = false StringUtils.equals("abc", null) = false StringUtils.equals("abc", "abc") = true StringUtils.equals("abc", "ABC") = false <br /> <br /> @param str1 the first String, may be null <br /> @param str2 the second String, may be null <br /> @return true if the Strings are equal, case sensitive, or <br /> both null <br /> @see java.lang.String#equals(Object) <br /> public static boolean equals(String str1, String str2) <br /> <br /> <br /> 10、 <br /> <br /> Compares two Strings, returning true if they are equal ignoring <br /> <br /> the case. <br /> <br /> <br /> nulls are handled without exceptions. Two null <br /> <br /> references are considered equal. Comparison is case insensitive. <br /> <br /> <br /> 判断两个字符串是否相等,有非空处理。忽略大小写 StringUtils.equalsIgnoreCase(null, null) = true StringUtils.equalsIgnoreCase(null, "abc") = false StringUtils.equalsIgnoreCase("abc", null) = false StringUtils.equalsIgnoreCase("abc", "abc") = true StringUtils.equalsIgnoreCase("abc", "ABC") = true <br /> <br /> @param str1 the first String, may be null <br /> @param str2 the second String, may be null <br /> @return true if the Strings are equal, case insensitive, or <br /> both null <br /> @see java.lang.String#equalsIgnoreCase(String) <br /> public static boolean equalsIgnoreCase(String str1, String str2) <br /> <br /> <br /> 六、 IndexOf 处理 <br /> 11、 <br /> <br /> <br /> Finds the first index within a String, handling null. <br /> <br /> This method uses {@link String#indexOf(String)}. <br /> <br /> <br /> A null String will return -1. <br /> <br /> <br /> 返回要查找的字符串所在位置,有非空处理 StringUtils.indexOf(null, *) = -1 StringUtils.indexOf(*, null) = -1 StringUtils.indexOf("", "") = 0 StringUtils.indexOf("aabaabaa", "a") = 0 StringUtils.indexOf("aabaabaa", "b") = 2 StringUtils.indexOf("aabaabaa", "ab") = 1 StringUtils.indexOf("aabaabaa", "") = 0 <br /> <br /> @param str the String to check, may be null <br /> @param searchStr the String to find, may be null <br /> @return the first index of the search String, <br /> -1 if no match or null string input <br /> @since 2.0 <br /> public static int indexOf(String str, String searchStr) <br /> <br /> 12、 <br /> <br /> Finds the first index within a String, handling null. <br /> <br /> This method uses {@link String#indexOf(String, int)}. <br /> <br /> <br /> A null String will return -1. <br /> <br /> A negative start position is treated as zero. <br /> An empty ("") search String always matches. <br /> A start position greater than the string length only matches <br /> an empty search String. <br /> <br /> <br /> 返回要由指定位置开始查找的字符串所在位置,有非空处理 StringUtils.indexOf(null, *, *) = -1 StringUtils.indexOf(*, null, *) = -1 StringUtils.indexOf("", "", 0) = 0 StringUtils.indexOf("aabaabaa", "a", 0) = 0 StringUtils.indexOf("aabaabaa", "b", 0) = 2 StringUtils.indexOf("aabaabaa", "ab", 0) = 1 StringUtils.indexOf("aabaabaa", "b", 3) = 5 StringUtils.indexOf("aabaabaa", "b", 9) = -1 StringUtils.indexOf("aabaabaa", "b", -1) = 2 StringUtils.indexOf("aabaabaa", "", 2) = 2 StringUtils.indexOf("abc", "", 9) = 3 <br /> <br /> @param str the String to check, may be null <br /> @param searchStr the String to find, may be null <br /> @param startPos the start position, negative treated as zero <br /> @return the first index of the search String, <br /> -1 if no match or null string input <br /> @since 2.0 <br /> public static int indexOf(String str, String searchStr, int startPos) <br /> <br /> <br /> 七、 子字符串处理: <br /> 13、 <br /> Gets a substring from the specified String avoiding exceptions. <br /> <br /> <br /> A negative start position can be used to start n <br /> <br /> characters from the end of the String. <br /> <br /> <br /> A null String will return null. <br /> <br /> An empty ("") String will return "". <br /> <br /> <br /> 返回指定位置开始的字符串中的所有字符 StringUtils.substring(null, *) = null StringUtils.substring("", *) = "" StringUtils.substring("abc", 0) = "abc" StringUtils.substring("abc", 2) = "c" StringUtils.substring("abc", 4) = "" StringUtils.substring("abc", -2) = "bc" StringUtils.substring("abc", -4) = "abc" <br /> <br /> @param str the String to get the substring from, may be null <br /> @param start the position to start from, negative means <br /> count back from the end of the String by this many characters <br /> @return substring from start position, null if null String input <br /> public static String substring(String str, int start) <br /> <br /> 14、 <br /> <br /> Gets a substring from the specified String avoiding exceptions. <br /> <br /> <br /> A negative start position can be used to start/end n <br /> <br /> characters from the end of the String. <br /> <br /> <br /> The returned substring starts with the character in the start <br /> <br /> position and ends before the end position. All postion counting is <br /> zero-based -- i.e., to start at the beginning of the string use <br /> start = 0. Negative start and end positions can be used to <br /> specify offsets relative to the end of the String. <br /> <br /> <br /> If start is not strictly to the left of end, "" <br /> <br /> is returned. <br /> <br /> <br /> 返回由开始位置到结束位置之间的子字符串 StringUtils.substring(null, *, *) = null StringUtils.substring("", * , *) = ""; StringUtils.substring("abc", 0, 2) = "ab" StringUtils.substring("abc", 2, 0) = "" StringUtils.substring("abc", 2, 4) = "c" StringUtils.substring("abc", 4, 6) = "" StringUtils.substring("abc", 2, 2) = "" StringUtils.substring("abc", -2, -1) = "b" StringUtils.substring("abc", -4, 2) = "ab" <br /> <br /> @param str the String to get the substring from, may be null <br /> @param start the position to start from, negative means <br /> count back from the end of the String by this many characters <br /> @param end the position to end at (exclusive), negative means <br /> count back from the end of the String by this many characters <br /> @return substring from start position to end positon, <br /> null if null String input <br /> public static String substring(String str, int start, int end) <br /> <br /> <br /> 15、 SubStringAfter/SubStringBefore(前后子字符串处理: <br /> <br /> <br /> Gets the substring before the first occurance of a separator. <br /> <br /> The separator is not returned. <br /> <br /> <br /> A null string input will return null. <br /> <br /> An empty ("") string input will return the empty string. <br /> A null separator will return the input string. <br /> <br /> <br /> 返回指定字符串之前的所有字符 StringUtils.substringBefore(null, *) = null StringUtils.substringBefore("", *) = "" StringUtils.substringBefore("abc", "a") = "" StringUtils.substringBefore("abcba", "b") = "a" StringUtils.substringBefore("abc", "c") = "ab" StringUtils.substringBefore("abc", "d") = "abc" StringUtils.substringBefore("abc", "") = "" StringUtils.substringBefore("abc", null) = "abc" <br /> <br /> @param str the String to get a substring from, may be null <br /> @param separator the String to search for, may be null <br /> @return the substring before the first occurance of the separator, <br /> null if null String input <br /> @since 2.0 <br /> public static String substringBefore(String str, String separator) <br /> <br /> 16、 <br /> <br /> Gets the substring after the first occurance of a separator. <br /> <br /> The separator is not returned. <br /> <br /> <br /> A null string input will return null. <br /> <br /> An empty ("") string input will return the empty string. <br /> A null separator will return the empty string if the <br /> input string is not null. <br /> <br /> <br /> 返回指定字符串之后的所有字符 StringUtils.substringAfter(null, *) = null StringUtils.substringAfter("", *) = "" StringUtils.substringAfter(*, null) = "" StringUtils.substringAfter("abc", "a") = "bc" StringUtils.substringAfter("abcba", "b") = "cba" StringUtils.substringAfter("abc", "c") = "" StringUtils.substringAfter("abc", "d") = "" StringUtils.substringAfter("abc", "") = "abc" <br /> <br /> @param str the String to get a substring from, may be null <br /> @param separator the String to search for, may be null <br /> @return the substring after the first occurance of the separator, <br /> null if null String input <br /> @since 2.0 <br /> public static String substringAfter(String str, String separator) <br /> <br /> 17、 <br /> <br /> Gets the substring before the last occurance of a separator. <br /> <br /> The separator is not returned. <br /> <br /> <br /> A null string input will return null. <br /> <br /> An empty ("") string input will return the empty string. <br /> An empty or null separator will return the input string. <br /> <br /> <br /> 返回最后一个指定字符串之前的所有字符 StringUtils.substringBeforeLast(null, *) = null StringUtils.substringBeforeLast("", *) = "" StringUtils.substringBeforeLast("abcba", "b") = "abc" StringUtils.substringBeforeLast("abc", "c") = "ab" StringUtils.substringBeforeLast("a", "a") = "" StringUtils.substringBeforeLast("a", "z") = "a" StringUtils.substringBeforeLast("a", null) = "a" StringUtils.substringBeforeLast("a", "") = "a" <br /> <br /> @param str the String to get a substring from, may be null <br /> @param separator the String to search for, may be null <br /> @return the substring before the last occurance of the separator, <br /> null if null String input <br /> @since 2.0 <br /> public static String substringBeforeLast(String str, String separator) <br /> <br /> 18、 <br /> <br /> Gets the substring after the last occurance of a separator. <br /> <br /> The separator is not returned. <br /> <br /> <br /> A null string input will return null. <br /> <br /> An empty ("") string input will return the empty string. <br /> An empty or null separator will return the empty string if <br /> the input string is not null. <br /> <br /> <br /> 返回最后一个指定字符串之后的所有字符 StringUtils.substringAfterLast(null, *) = null StringUtils.substringAfterLast("", *) = "" StringUtils.substringAfterLast(*, "") = "" StringUtils.substringAfterLast(*, null) = "" StringUtils.substringAfterLast("abc", "a") = "bc" StringUtils.substringAfterLast("abcba", "b") = "a" StringUtils.substringAfterLast("abc", "c") = "" StringUtils.substringAfterLast("a", "a") = "" StringUtils.substringAfterLast("a", "z") = "" <br /> <br /> @param str the String to get a substring from, may be null <br /> @param separator the String to search for, may be null <br /> @return the substring after the last occurance of the separator, <br /> null if null String input <br /> @since 2.0 <br /> public static String substringAfterLast(String str, String separator) <br /> <br /> <br /> 八、 Replacing(字符串替换) <br /> 19、 <br /> Replaces all occurances of a String within another String. <br /> <br /> <br /> A null reference passed to this method is a no-op. <br /> <br /> <br /> 以指定字符串替换原来字符串的的指定字符串 StringUtils.replace(null, *, *) = null StringUtils.replace("", *, *) = "" StringUtils.replace("aba", null, null) = "aba" StringUtils.replace("aba", null, null) = "aba" StringUtils.replace("aba", "a", null) = "aba" StringUtils.replace("aba", "a", "") = "aba" StringUtils.replace("aba", "a", "z") = "zbz" <br /> <br /> @param text text to search and replace in, may be null <br /> @param repl the String to search for, may be null <br /> @param with the String to replace with, may be null <br /> @return the text with any replacements processed, <br /> null if null String input <br /> @see #replace(String text, String repl, String with, int max) <br /> public static String replace(String text, String repl, String with) <br /> <br /> 20、 <br /> <br /> Replaces a String with another String inside a larger String, <br /> <br /> for the first max values of the search String. <br /> <br /> <br /> A null reference passed to this method is a no-op. <br /> <br /> <br /> 以指定字符串最大替换原来字符串的的指定字符串 StringUtils.replace(null, *, *, *) = null StringUtils.replace("", *, *, *) = "" StringUtils.replace("abaa", null, null, 1) = "abaa" StringUtils.replace("abaa", null, null, 1) = "abaa" StringUtils.replace("abaa", "a", null, 1) = "abaa" StringUtils.replace("abaa", "a", "", 1) = "abaa" 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" <br /> <br /> @param text text to search and replace in, may be null <br /> @param repl the String to search for, may be null <br /> @param with the String to replace with, may be null <br /> @param max maximum number of values to replace, or -1 if no maximum <br /> @return the text with any replacements processed, <br /> null if null String input <br /> public static String replace(String text, String repl, String with, int max) <br /> <br /> <br /> 九、 Case conversion(大小写转换) <br /> 21、 <br /> <br /> Converts a String to upper case as per {@link String#toUpperCase()}. <br /> <br /> <br /> A null input String returns null. <br /> <br /> <br /> 将一个字符串变为大写 StringUtils.upperCase(null) = null StringUtils.upperCase("") = "" StringUtils.upperCase("aBc") = "ABC" <br /> <br /> @param str the String to upper case, may be null <br /> @return the upper cased String, null if null String input <br /> public static String upperCase(String str) 22、 <br /> <br /> Converts a String to lower case as per {@link String#toLowerCase()}. <br /> <br /> <br /> A null input String returns null. <br /> <br /> <br /> 将一个字符串转换为小写 StringUtils.lowerCase(null) = null StringUtils.lowerCase("") = "" StringUtils.lowerCase("aBc") = "abc" <br /> <br /> @param str the String to lower case, may be null <br /> @return the lower cased String, null if null String input <br /> public static String lowerCase(String str) 23、 <br /> <br /> Capitalizes a String changing the first letter to title case as <br /> <br /> per {@link Character#toTitleCase(char)}. No other letters are changed. <br /> <br /> <br /> For a word based alorithm, see {@link /WordUtils#capitalize(String)}. <br /> <br /> A null input String returns null. <br /> <br /> <br /> StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" <br /> <br /> @param str the String to capitalize, may be null <br /> @return the capitalized String, null if null String input <br /> @see /WordUtils#capitalize(String) <br /> @see /uncapitalize(String) <br /> @since 2.0 <br /> 将字符串中的首字母大写 <br /> public static String capitalize(String str) <br /> </p>