﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-微笑沙漠-随笔分类-commons</title><link>http://www.blogjava.net/cyantide/category/36405.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 01 Dec 2008 16:23:38 GMT</lastBuildDate><pubDate>Mon, 01 Dec 2008 16:23:38 GMT</pubDate><ttl>60</ttl><item><title>org.apache.commons.lang.StringUtil的使用(转帖)</title><link>http://www.blogjava.net/cyantide/archive/2008/07/21/216341.html</link><dc:creator>微笑沙漠</dc:creator><author>微笑沙漠</author><pubDate>Mon, 21 Jul 2008 05:47:00 GMT</pubDate><guid>http://www.blogjava.net/cyantide/archive/2008/07/21/216341.html</guid><wfw:comment>http://www.blogjava.net/cyantide/comments/216341.html</wfw:comment><comments>http://www.blogjava.net/cyantide/archive/2008/07/21/216341.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cyantide/comments/commentRss/216341.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cyantide/services/trackbacks/216341.html</trackback:ping><description><![CDATA[<div class="blog_content">StringUtil包函数 <br />
<br />
<br />
<strong>1.空字符串检查</strong><br />
使用函数: StringUtils.isBlank(testString)<br />
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False<br />
例程:<br />
&nbsp;&nbsp;&nbsp; String test = "";<br />
&nbsp;&nbsp;&nbsp; String test2 = "\n\n\t";<br />
&nbsp;&nbsp;&nbsp; String test3 = null;<br />
&nbsp;&nbsp;&nbsp; String test4 = "Test";
<p>&nbsp;&nbsp;&nbsp; System.out.println( "test blank? " + StringUtils.isBlank( test ) );<br />
&nbsp;&nbsp;&nbsp; System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );<br />
&nbsp;&nbsp;&nbsp; System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );<br />
&nbsp;&nbsp;&nbsp; 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 />
&nbsp;&nbsp;&nbsp; String test1 = "\t";<br />
&nbsp;&nbsp;&nbsp; String test2 = "&nbsp; A&nbsp; Test&nbsp; ";<br />
&nbsp;&nbsp;&nbsp; String test3 = null;</p>
<p>&nbsp;&nbsp;&nbsp; System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) );<br />
&nbsp;&nbsp;&nbsp; System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) );<br />
&nbsp;&nbsp;&nbsp; System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );</p>
<p>输出如下:<br />
test1 trimToNull: null<br />
test2 trimToNull: A&nbsp; 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 />
&nbsp;&nbsp;&nbsp; String test = "This is a test of the abbreviation.";<br />
&nbsp;&nbsp;&nbsp; String test2 = "Test";</p>
<p>&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.abbreviate( test, 15 ) );<br />
&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.abbreviate( test, 5,15 ) );<br />
&nbsp;&nbsp;&nbsp; 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 />
&nbsp;&nbsp;&nbsp; String input = "A b,c.d|e";<br />
&nbsp;&nbsp;&nbsp; String input2 = "Pharmacy, basketball funky";<br />
&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp; String[] array1 = StringUtils.split( input, " ,.|");<br />
&nbsp;&nbsp;&nbsp; String[] array2 = StringUtils.split( input2, " ,", 2 );</p>
<p><br />
&nbsp;&nbsp;&nbsp; System.out.println( ArrayUtils.toString( array1 ) );<br />
&nbsp;&nbsp;&nbsp; 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 />
&nbsp;&nbsp;&nbsp; String htmlContent = "ABC1234ABC4567";<br />
&nbsp;&nbsp;&nbsp; System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));<br />
&nbsp;&nbsp;&nbsp; System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));<br />
输出如下：<br />
&nbsp;&nbsp;&nbsp; ABC<br />
&nbsp;&nbsp;&nbsp; null</p>
<p><br />
<strong>6.去除尾部换行符</strong><br />
使用函数:StringUtils.chomp(testString)<br />
函数介绍:去除testString尾部的换行符<br />
例程:<br />
&nbsp;&nbsp;&nbsp; String input = "Hello\n";<br />
&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.chomp( input ));<br />
&nbsp;&nbsp;&nbsp; String input2 = "Another test\r\n";<br />
&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.chomp( input2 ));<br />
输出如下:<br />
&nbsp;&nbsp;&nbsp; Hello<br />
&nbsp;&nbsp;&nbsp; Another test</p>
<p><br />
<strong>7.重复字符串</strong><br />
使用函数:StringUtils.repeat(repeatString,count)<br />
函数介绍:得到将repeatString重复count次后的字符串<br />
例程:<br />
&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.repeat( "*", 10));<br />
&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.repeat( "China ", 5));<br />
输出如下:<br />
&nbsp;&nbsp;&nbsp; **********<br />
&nbsp;&nbsp;&nbsp; China China China China China </p>
<p>其他函数:StringUtils.center( testString, count,repeatString );<br />
函数介绍:把testString插入将repeatString重复多次后的字符串中间,得到字符串<br />
的总长为count<br />
例程:<br />
&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.center( "China", 11,"*"));<br />
输出如下:<br />
&nbsp;&nbsp;&nbsp; ***China***</p>
<p><br />
<strong>8.颠倒字符串</strong><br />
使用函数:StringUtils.reverse(testString)<br />
函数介绍:得到testString中字符颠倒后的字符串<br />
例程:<br />
&nbsp;&nbsp;&nbsp; System.out.println( StringUtils.reverse("ABCDE"));<br />
输出如下:<br />
&nbsp;&nbsp;&nbsp; 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 )&nbsp; :如果testString全由字母或空格组<br />
成返回True</p>
<p>例程:<br />
&nbsp;&nbsp;&nbsp; String state = "Virginia";<br />
&nbsp;&nbsp;&nbsp; System.out.println( "Is state number? " + StringUtils.isNumeric(<br />
state ) );<br />
&nbsp;&nbsp;&nbsp; System.out.println( "Is state alpha? " + StringUtils.isAlpha( state )<br />
);<br />
&nbsp;&nbsp;&nbsp; System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );<br />
&nbsp;&nbsp;&nbsp; System.out.println( "Is state alphaspace? " + StringUtils.isAlphaSpace( state ) );<br />
输出如下:<br />
&nbsp;&nbsp;&nbsp; Is state number? false<br />
&nbsp;&nbsp;&nbsp; Is state alpha? true<br />
&nbsp;&nbsp;&nbsp; Is state alphanumeric? true<br />
&nbsp;&nbsp;&nbsp; Is state alphaspace? true</p>
<p><strong>10.取得某字符串在另一字符串中出现的次数<br />
</strong>使用函数:StringUtils.countMatches(testString,seqString)<br />
函数介绍:取得seqString在testString中出现的次数,未发现则返回零<br />
例程:<br />
&nbsp;&nbsp;&nbsp; System.out.println(StringUtils.countMatches( "Chinese People", "e"<br />
));<br />
输出:<br />
&nbsp;&nbsp;&nbsp; 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 />
&nbsp;&nbsp;&nbsp; String formatted = " 25 * (30,40) [50,60] | 30";<br />
&nbsp;&nbsp;&nbsp; System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );<br />
&nbsp;&nbsp;&nbsp; System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(", "," ) );<br />
&nbsp;&nbsp;&nbsp; System.out.print(", N2: " + StringUtils.substringBetween( formatted, ",", ")" ) );<br />
&nbsp;&nbsp;&nbsp; System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[", "," ) );<br />
&nbsp;&nbsp;&nbsp; System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",", "]" ) );<br />
&nbsp;&nbsp;&nbsp; System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) );<br />
输出如下：<br />
&nbsp;&nbsp;&nbsp; N0:&nbsp; 25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5:&nbsp; 30</p>
<br />
<br />
<br />
<br />
一、数组转成字符串： <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 &lt;= 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 &lt;= 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 &lt;= 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 &lt;= 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 &lt;= 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 &lt;= 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 &lt;= 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) </div>
 <img src ="http://www.blogjava.net/cyantide/aggbug/216341.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cyantide/" target="_blank">微笑沙漠</a> 2008-07-21 13:47 <a href="http://www.blogjava.net/cyantide/archive/2008/07/21/216341.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>