Update Javadoc and tests

bug 21750, from Phil Steitz


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137479 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-20 23:57:26 +00:00
parent 82c4c69189
commit d381baaf33
4 changed files with 225 additions and 113 deletions

View File

@ -114,7 +114,7 @@
* <li>empty - a zero-length string (<code>""</code>)
* <li>space - the space character (<code>' '</code>)(char 32)
* <li>whitespace - the characters defined by {@link Character#isWhitespace(char)}
* <li>trim - the characters &lt;= 32 as in {@link String#trim(String)}
* <li>trim - the characters &lt;= 32 as in {@link String#trim()}
* </ul>
*
* <p><code>StringUtils</code> handles <code>null</code> input Strings quietly.
@ -141,8 +141,9 @@
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author Arun Mammen Thomas
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Phil Steitz
* @since 1.0
* @version $Id: StringUtils.java,v 1.74 2003/07/20 15:41:52 scolebourne Exp $
* @version $Id: StringUtils.java,v 1.75 2003/07/20 23:57:26 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -192,8 +193,7 @@ public StringUtils() {
//-----------------------------------------------------------------------
/**
* <p>Checks if a String is empty ("") or null.
* <code>null</code> returns <code>false</code></p>
* <p>Checks if a String is empty ("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
@ -260,7 +260,7 @@ public static boolean isBlank(String str) {
}
/**
* <p>Checks if a String is not empty ("") and not null.</p>
* <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
*
* <pre>
* StringUtils.isNotBlank(null) = false
@ -612,7 +612,7 @@ public static String stripEnd(String str, String stripChars) {
* StringUtils.stripAll(["abc ", null]) = ["abc", null]
* </pre>
*
* @param str the array to remove whitespace from, may be null
* @param strs the array to remove whitespace from, may be null
* @return the stripped Strings, <code>null</code> if null array input
*/
public static String[] stripAll(String[] strs) {
@ -640,7 +640,7 @@ public static String[] stripAll(String[] strs) {
* StringUtils.stripAll(["yabcz", null], "yz") = ["abc", null]
* </pre>
*
* @param str the array to remove characters from, may be null
* @param strs the array to remove characters from, may be null
* @param stripChars the characters to remove, null treated as whitespace
* @return the stripped Strings, <code>null</code> if null array input
*/
@ -713,13 +713,13 @@ public static boolean equalsIgnoreCase(String str1, String str2) {
/**
* <p>Finds the first index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(int)}</p>
* This method uses {@link String#indexOf(int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.</p>
* <p>A <code>null</code> or empty ("") String will return <code>-1</code>.</p>
*
* <pre>
* StringUtils.indexOf(null, ' ') = -1
* StringUtils.indexOf("", ' ') = -1
* StringUtils.indexOf(null, *) = -1
* StringUtils.indexOf("", *) = -1
* StringUtils.indexOf("aabaabaa", 'a') = 0
* StringUtils.indexOf("aabaabaa", 'b') = 2
* </pre>
@ -739,17 +739,15 @@ public static int indexOf(String str, char searchChar) {
/**
* <p>Finds the first index within a String from a start position,
* handling <code>null</code>.
* This method uses {@link String#indexOf(int, int)}</p>
* This method uses {@link String#indexOf(int, int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.
* <p>A <code>null</code> or empty ("") String will return <code>-1</code>.
* A negative start position is treated as zero.
* A start position greater than the string length returns <code>-1</code>.</p>
*
* <pre>
* StringUtils.indexOf(null, ' ', 0) = -1
* StringUtils.indexOf(null, ' ', -1) = -1
* StringUtils.indexOf("", ' ', 0) = -1
* StringUtils.indexOf("", ' ', -1) = -1
* StringUtils.indexOf(null, *, *) = -1
* StringUtils.indexOf("", *, *) = -1
* StringUtils.indexOf("aabaabaa", 'b', 0) = 2
* StringUtils.indexOf("aabaabaa", 'b', 3) = 5
* StringUtils.indexOf("aabaabaa", 'b', 9) = -1
@ -771,17 +769,18 @@ public static int indexOf(String str, char searchChar, int startPos) {
/**
* <p>Finds the first index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(String)}</p>
* This method uses {@link String#indexOf(String)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.</p>
*
* <pre>
* StringUtils.indexOf(null, null) = -1
* StringUtils.indexOf("", null) = -1
* 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
* </pre>
*
* @param str the String to check, may be null
@ -798,7 +797,7 @@ public static int indexOf(String str, String searchStr) {
/**
* <p>Finds the first index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(String, int)}</p>
* This method uses {@link String#indexOf(String, int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.
* A negative start position is treated as zero.
@ -807,10 +806,8 @@ public static int indexOf(String str, String searchStr) {
* an empty search String.</p>
*
* <pre>
* StringUtils.indexOf(null, null, 0) = -1
* StringUtils.indexOf(null, null, -1) = -1
* StringUtils.indexOf("", null, 0) = -1
* StringUtils.indexOf("", null, -1) = -1
* StringUtils.indexOf(null, *, *) = -1
* StringUtils.indexOf(*, null, *) = -1
* StringUtils.indexOf("", "", 0) = 0
* StringUtils.indexOf("aabaabaa", "a", 0) = 0
* StringUtils.indexOf("aabaabaa", "b", 0) = 2
@ -818,6 +815,7 @@ public static int indexOf(String str, String searchStr) {
* StringUtils.indexOf("aabaabaa", "b", 3) = 5
* StringUtils.indexOf("aabaabaa", "b", 9) = -1
* StringUtils.indexOf("aabaabaa", "b", -1) = 2
* StringUtils.indexOf("aabaabaa", "", 2) = 2
* </pre>
*
* @param str the String to check, may be null
@ -838,13 +836,13 @@ public static int indexOf(String str, String searchStr, int startPos) {
/**
* <p>Finds the last index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(int)}</p>
* This method uses {@link String#lastIndexOf(int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.</p>
* <p>A <code>null</code> or empty ("") String will return <code>-1</code>.</p>
*
* <pre>
* StringUtils.lastIndexOf(null, ' ') = -1
* StringUtils.lastIndexOf("", ' ') = -1
* StringUtils.lastIndexOf(null, *) = -1
* StringUtils.lastIndexOf("", *) = -1
* StringUtils.lastIndexOf("aabaabaa", 'a') = 7
* StringUtils.lastIndexOf("aabaabaa", 'b') = 5
* </pre>
@ -864,22 +862,21 @@ public static int lastIndexOf(String str, char searchChar) {
/**
* <p>Finds the last index within a String from a start position,
* handling <code>null</code>.
* This method uses {@link String#indexOf(int, int)}</p>
* This method uses {@link String#lastIndexOf(int, int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.
* A negative or zero start position returns <code>-1</code>.
* <p>A <code>null</code> or empty ("") String will return <code>-1</code>.
* A negative start position returns <code>-1</code>.
* A start position greater than the string length searches the whole string.</p>
*
* <pre>
* StringUtils.lastIndexOf(null, ' ', 0) = -1
* StringUtils.lastIndexOf(null, ' ', -1) = -1
* StringUtils.lastIndexOf("", ' ', 0) = -1
* StringUtils.lastIndexOf("", ' ', -1) = -1
* StringUtils.lastIndexOf(null, *, *) = -1
* StringUtils.lastIndexOf("", *, *) = -1
* StringUtils.lastIndexOf("aabaabaa", 'b', 8) = 5
* StringUtils.lastIndexOf("aabaabaa", 'b', 4) = 2
* StringUtils.lastIndexOf("aabaabaa", 'b', 0) = -1
* StringUtils.lastIndexOf("aabaabaa", 'b', 9) = 5
* StringUtils.lastIndexOf("aabaabaa", 'b', -1) = -1
* StringUtils.lastIndexOf("aabaabaa", 'a', 0) = 0
* </pre>
*
* @param str the String to check, may be null
@ -897,17 +894,18 @@ public static int lastIndexOf(String str, char searchChar, int startPos) {
/**
* <p>Finds the last index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(String)}</p>
* This method uses {@link String#lastIndexOf(String)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.</p>
*
* <pre>
* StringUtils.lastIndexOf(null, null) = -1
* StringUtils.lastIndexOf("", null) = -1
* StringUtils.lastIndexOf(null, *) = -1
* StringUtils.lastIndexOf(*, null) = -1
* StringUtils.lastIndexOf("", "") = 0
* StringUtils.lastIndexOf("aabaabaa", "a") = 0
* StringUtils.lastIndexOf("aabaabaa", "b") = 2
* StringUtils.lastIndexOf("aabaabaa", "ab") = 1
* StringUtils.lastIndexOf("aabaabaa", "") = 8
* </pre>
*
* @param str the String to check, may be null
@ -924,24 +922,23 @@ public static int lastIndexOf(String str, String searchStr) {
/**
* <p>Finds the first index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(String, int)}</p>
* This method uses {@link String#lastIndexOf(String, int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.
* A negative start position returns <code>-1</code>.
* A zero start position only matches an empty String ("").
* An empty ("") search String always matches unless start position negative.
* An empty ("") search String always matches unless the start position is negative.
* A start position greater than the string length searches the whole string.</p>
*
* <pre>
* StringUtils.lastIndexOf(null, null, 0) = -1
* StringUtils.lastIndexOf(null, null, -1) = -1
* StringUtils.lastIndexOf("", null, 0) = -1
* StringUtils.lastIndexOf("", null, -1) = -1
* StringUtils.lastIndexOf(null, *, *) = -1
* StringUtils.lastIndexOf(*, null, *) = -1
* StringUtils.lastIndexOf("aabaabaa", "a", 8) = 7
* StringUtils.lastIndexOf("aabaabaa", "b", 8) = 5
* StringUtils.lastIndexOf("aabaabaa", "ab", 8) = 4
* StringUtils.lastIndexOf("aabaabaa", "b", 9) = 5
* StringUtils.lastIndexOf("aabaabaa", "b", -1) = -1
* StringUtils.lastIndexOf("aabaabaa", "a", 0) = 0
* StringUtils.lastIndexOf("aabaabaa", "b", 0) = -1
* </pre>
*
* @param str the String to check, may be null
@ -962,13 +959,13 @@ public static int lastIndexOf(String str, String searchStr, int startPos) {
/**
* <p>Checks if String contains a search character, handling <code>null</code>.
* This method uses {@link String#indexOf(int)}</p>
* This method uses {@link String#indexOf(int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.</p>
* <p>A <code>null</code> or empty ("") String will return <code>false</code>.</p>
*
* <pre>
* StringUtils.contains(null, ' ') = false
* StringUtils.contains("", ' ') = false
* StringUtils.contains(null, *) = false
* StringUtils.contains("", *) = false
* StringUtils.contains("abc", 'a') = true
* StringUtils.contains("abc", 'z') = false
* </pre>
@ -987,13 +984,13 @@ public static boolean contains(String str, char searchChar) {
/**
* <p>Find the first index within a String, handling <code>null</code>.
* This method uses {@link String#indexOf(String)}</p>
* This method uses {@link String#indexOf(int)}.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.</p>
* <p>A <code>null</code> String will return <code>false</code>.</p>
*
* <pre>
* StringUtils.contains(null, null) = false
* StringUtils.contains("", null) = false
* StringUtils.contains(null, *) = false
* StringUtils.contains(*, null) = false
* StringUtils.contains("", "") = true
* StringUtils.contains("abc", "") = true
* StringUtils.contains("abc", "a") = true
@ -1019,19 +1016,22 @@ public static boolean contains(String str, String searchStr) {
* <p>Find the first index of any of a set of potential substrings.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.
* A <code>null</code> search array will return <code>-1</code>.
* A <code>null</code> search array entry will be ignored.</p>
* A <code>null</code> or zero length search array will return <code>-1</code>.
* A <code>null</code> search array entry will be ignored, but a search
* array containing "" will return <code>0</code> if <code>str</code> is not
* null. This method uses {@link String#indexOf(String)}.</p>
*
* <pre>
* StringUtils.indexOfAny(null, null) = -1
* StringUtils.indexOfAny(null, ["ab","cd"]) = -1
* StringUtils.indexOfAny("", null) = -1
* StringUtils.indexOfAny("", ["ab","cd"]) = -1
* StringUtils.indexOfAny("zzabyycdxx", null) = -1
* StringUtils.indexOfAny("zzabyycdxx", []) = -1
* StringUtils.indexOfAny(null, *) = -1
* StringUtils.indexOfAny(*, null) = -1
* StringUtils.indexOfAny(*, []) = -1
* StringUtils.indexOfAny("zzabyycdxx", ["ab","cd"]) = 2
* StringUtils.indexOfAny("zzabyycdxx", ["cd","ab"]) = 2
* StringUtils.indexOfAny("zzabyycdxx", ["mn","op"]) = -1
* StringUtils.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1
* StringUtils.indexOfAny("zzabyycdxx", [""]) = 0
* StringUtils.indexOfAny("", [""]) = 0
* StringUtils.indexOfAny("", ["a"]) = -1
* </pre>
*
* @param str the String to check, may be null
@ -1071,18 +1071,20 @@ public static int indexOfAny(String str, String[] searchStrs) {
*
* <p>A <code>null</code> String will return <code>-1</code>.
* A <code>null</code> search array will return <code>-1</code>.
* A <code>null</code> search array entry will be ignored.</p>
* A <code>null</code> or zero length search array entry will be ignored,
* but a search array containing "" will return the length of <code>str</code>
* if <code>str</code> is not null. This method uses {@link String#indexOf(String)}</p>
*
* <pre>
* StringUtils.lastIndexOfAny(null, null) = -1
* StringUtils.lastIndexOfAny(null, ["ab","cd"]) = -1
* StringUtils.lastIndexOfAny("", null) = -1
* StringUtils.lastIndexOfAny("", ["ab","cd"]) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", null) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", []) = -1
* StringUtils.lastIndexOfAny(null, *) = -1
* StringUtils.lastIndexOfAny(*, null) = -1
* StringUtils.lastIndexOfAny(*, []) = -1
* StringUtils.lastIndexOfAny(*, [null]) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", ["ab","cd"]) = 6
* StringUtils.lastIndexOfAny("zzabyycdxx", ["cd","ab"]) = 6
* StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", ["mn",""]) = 10
* </pre>
*
* @param str the String to check, may be null
@ -1116,6 +1118,17 @@ public static int lastIndexOfAny(String str, String[] searchStrs) {
* <p>Search a String to find the first index of any
* character not in the given set of characters.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.
* A <code>null</code> or zero length search array will return <code>-1</code>.</p>
*
* <pre>
* StringUtils.indexOfAnyBut(null, *) = -1
* StringUtils.indexOfAnyBut(*, null) = -1
* StringUtils.indexOfAnyBut("zzabyycdxx",'za') = 3
* StringUtils.indexOfAnyBut("zzabyycdxx", '') = 0
* StringUtils.indexOfAnyBut("aba", 'ab') = -1
* </pre>
*
* @param str the String to check, may be null
* @param searchChars the chars to search for, may be null
* @return the index of any of the chars, -1 if no match or null input
@ -1131,6 +1144,17 @@ public static int indexOfAnyBut(String str, char[] searchChars) {
* <p>Search a String to find the first index of any
* character not in the given set of characters.</p>
*
* <p>A <code>null</code> String will return <code>-1</code>.
* A <code>null</code> search string will return <code>-1</code>.</p>
*
* <pre>
* StringUtils.indexOfAnyBut(null, *) = -1
* StringUtils.indexOfAnyBut(*, null) = -1
* StringUtils.indexOfAnyBut("zzabyycdxx", "za") = 3
* StringUtils.indexOfAnyBut("zzabyycdxx", "") = 0
* StringUtils.indexOfAnyBut("aba","ab") = -1
* </pre>
*
* @param str the String to check, may be null
* @param searchChars the chars to search for, may be null
* @return the index of any of the chars, -1 if no match or null input
@ -1155,11 +1179,14 @@ public static int indexOfAnyBut(String str, String searchChars) {
/**
* <p>Checks if the String contains only certain characters.</p>
*
* <p>A <code>null</code> String will return <code>false</code>.
* A <code>null</code> valid character array will return <code>false</code>.
* An empty String ("") always returns <code>true</code>.</p>
*
* <pre>
* StringUtils.containsOnly(null, 'abc') = false
* StringUtils.containsOnly("", null) = false
* StringUtils.containsOnly("", 'abc') = true
* StringUtils.containsOnly("", '') = true
* StringUtils.containsOnly(null, *) = false
* StringUtils.containsOnly(*, null) = false
* StringUtils.containsOnly("", *) = true
* StringUtils.containsOnly("ab", '') = false
* StringUtils.containsOnly("abab", 'abc') = true
* StringUtils.containsOnly("ab1", 'abc') = false
@ -1187,11 +1214,14 @@ public static boolean containsOnly(String str, char[] valid) {
/**
* <p>Checks if the String contains only certain characters.</p>
*
* <p>A <code>null</code> String will return <code>false</code>.
* A <code>null</code> valid character String will return <code>false</code>.
* An empty String ("") always returns <code>true</code>.</p>
*
* <pre>
* StringUtils.containsOnly(null, "abc") = false
* StringUtils.containsOnly("", null) = false
* StringUtils.containsOnly("", "abc") = true
* StringUtils.containsOnly("", "") = true
* StringUtils.containsOnly(null, *) = false
* StringUtils.containsOnly(*, null) = false
* StringUtils.containsOnly("", *) = true
* StringUtils.containsOnly("ab", "") = false
* StringUtils.containsOnly("abab", "abc") = true
* StringUtils.containsOnly("ab1", "abc") = false
@ -1215,15 +1245,18 @@ public static boolean containsOnly(String str, String validChars) {
/**
* <p>Checks that the String does not contain certain characters.</p>
*
* <p>A <code>null</code> String will return <code>true</code>.
* A <code>null</code> invalid character array will return <code>true</code>.
* An empty String ("") always returns true.</p>
*
* <pre>
* StringUtils.containsOnly(null, 'xyz') = true
* StringUtils.containsOnly("", null) = true
* StringUtils.containsOnly("", 'xyz') = true
* StringUtils.containsOnly("", '') = true
* StringUtils.containsOnly("ab", '') = true
* StringUtils.containsOnly("abab", 'xyz') = true
* StringUtils.containsOnly("ab1", 'xyz') = true
* StringUtils.containsOnly("abz", 'xyz') = false
* StringUtils.containsNone(null, *) = true
* StringUtils.containsNone(*, null) = true
* StringUtils.containsNone("", *) = true
* StringUtils.containsNone("ab", '') = true
* StringUtils.containsNone("abab", 'xyz') = true
* StringUtils.containsNone("ab1", 'xyz') = true
* StringUtils.containsNone("abz", 'xyz') = false
* </pre>
*
* @param str the String to check, may be null
@ -1250,15 +1283,18 @@ public static boolean containsNone(String str, char[] invalidChars) {
/**
* <p>Checks that the String does not contain certain characters.</p>
*
* <p>A <code>null</code> String will return <code>true</code>.
* A <code>null</code> invalid character array will return <code>true</code>.
* An empty String ("") always returns true.</p>
*
* <pre>
* StringUtils.containsOnly(null, "xyz") = true
* StringUtils.containsOnly("", null) = true
* StringUtils.containsOnly("", "xyz") = true
* StringUtils.containsOnly("", "") = true
* StringUtils.containsOnly("ab", "") = true
* StringUtils.containsOnly("abab", "xyz") = true
* StringUtils.containsOnly("ab1", "xyz") = true
* StringUtils.containsOnly("abz", "xyz") = false
* StringUtils.containsNone(null, *) = true
* StringUtils.containsNone(*, null) = true
* StringUtils.containsNone("", *) = true
* StringUtils.containsNone("ab", "") = true
* StringUtils.containsNone("abab", "xyz") = true
* StringUtils.containsNone("ab1", "xyz") = true
* StringUtils.containsNone("abz", "xyz") = false
* </pre>
*
* @param str the String to check, may be null
@ -1281,9 +1317,12 @@ public static boolean containsNone(String str, String invalidChars) {
* <p>A negative start position can be used to start <code>n</code>
* characters from the end of the String.</p>
*
* <p>A <code>null</code> String will return <code>null</code>.
* An empty ("") String will return "".</p>
*
* <pre>
* StringUtils.substring(null, 0) = null
* StringUtils.substring("", 0) = ""
* StringUtils.substring(null, *) = null
* StringUtils.substring("", *) = ""
* StringUtils.substring("abc", 0) = "abc"
* StringUtils.substring("abc", 2) = "c"
* StringUtils.substring("abc", 4) = ""
@ -1322,13 +1361,23 @@ public static String substring(String str, int start) {
* <p>A negative start position can be used to start/end <code>n</code>
* characters from the end of the String.</p>
*
* <p>The returned substring starts with the character in the <code>start</code>
* position and ends before the <code>end</code> position. All postion counting is
* zero-based -- i.e., to start at the beginning of the string use
* <code>start = 0</code>. Negative start and end positions can be used to
* specify offsets relative to the end of the String.</p>
*
* <p>If <code>start</code> is not strictly to the left of <code>end</code>, ""
* is returned.</p>
*
* <pre>
* StringUtils.substring(null, 0, 2) = null
* StringUtils.substring("", 0, 2) = ""
* 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"
* </pre>
@ -1502,6 +1551,7 @@ public static String mid(String str, int pos, int len) {
* StringUtils.split("") = []
* StringUtils.split("abc def") = ["abc", "def"]
* StringUtils.split("abc def") = ["abc", "def"]
* StringUtils.split(" abc ") = ["abc"]
* </pre>
*
* @param str the String to parse, may be null
@ -1526,10 +1576,12 @@ public static String[] split(String str) {
* StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
* StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
* StringUtils.split("a:b:c", '.') = ["a:b:c"]
* StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
* StringUtils.split("a b c", ' ') = ["a", "b", "c"]
* </pre>
*
* @param str the String to parse, may be null
* @param separatorChars the characters used as the delimiters,
* @param separatorChar the character used as the delimiter,
* <code>null</code> splits on whitespace
* @return an array of parsed Strings, <code>null</code> if null String input
*/
@ -2868,7 +2920,7 @@ public static String lowerCase(String str) {
/**
* <p>Capitalises a String changing the first letter to title case as
* per {@link Character#toTitleCase()}. No other letters are changed.</p>
* per {@link Character#toTitleCase(char)}. No other letters are changed.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.</p>
*
@ -2895,7 +2947,7 @@ public static String capitalise(String str) {
/**
* <p>Uncapitalises a String changing the first letter to title case as
* per {@link Character#toLowerCase()}. No other letters are changed.</p>
* per {@link Character#toLowerCase(char)}. No other letters are changed.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.</p>
*

View File

@ -63,7 +63,8 @@
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: StringUtilsEqualsIndexOfTest.java,v 1.3 2003/07/19 23:29:06 scolebourne Exp $
* @author Phil Steitz
* @version $Id: StringUtilsEqualsIndexOfTest.java,v 1.4 2003/07/20 23:57:26 scolebourne Exp $
*/
public class StringUtilsEqualsIndexOfTest extends TestCase {
private static final String FOO = "foo";
@ -142,6 +143,7 @@ public void testIndexOf_String() {
assertEquals(0, StringUtils.indexOf("aabaabaa", "a"));
assertEquals(2, StringUtils.indexOf("aabaabaa", "b"));
assertEquals(1, StringUtils.indexOf("aabaabaa", "ab"));
assertEquals(0, StringUtils.indexOf("aabaabaa", ""));
}
public void testIndexOf_StringInt() {
@ -163,6 +165,7 @@ public void testIndexOf_StringInt() {
assertEquals(5, StringUtils.indexOf("aabaabaa", "b", 3));
assertEquals(-1, StringUtils.indexOf("aabaabaa", "b", 9));
assertEquals(2, StringUtils.indexOf("aabaabaa", "b", -1));
assertEquals(2,StringUtils.indexOf("aabaabaa", "", 2));
}
//-----------------------------------------------------------------------
@ -183,12 +186,15 @@ public void testLastIndexOf_charInt() {
assertEquals(2, StringUtils.lastIndexOf("aabaabaa", 'b', 3));
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", 'b', 9));
assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", 'b', -1));
assertEquals(0, StringUtils.lastIndexOf("aabaabaa", 'a', 0));
}
public void testLastIndexOf_String() {
assertEquals(-1, StringUtils.lastIndexOf(null, null));
assertEquals(-1, StringUtils.lastIndexOf("", null));
assertEquals(-1, StringUtils.lastIndexOf("", "a"));
assertEquals(0, StringUtils.lastIndexOf("", ""));
assertEquals(8, StringUtils.lastIndexOf("aabaabaa", ""));
assertEquals(7, StringUtils.lastIndexOf("aabaabaa", "a"));
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b"));
assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab"));
@ -213,19 +219,23 @@ public void testLastIndexOf_StringInt() {
assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "b", 3));
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b", 9));
assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", -1));
assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", 0));
assertEquals(0, StringUtils.lastIndexOf("aabaabaa", "a", 0));
}
//-----------------------------------------------------------------------
public void contains_char() {
public void testContainsChar() {
assertEquals(false, StringUtils.contains(null, ' '));
assertEquals(false, StringUtils.contains("", ' '));
assertEquals(false, StringUtils.contains("",null));
assertEquals(false, StringUtils.contains(null,null));
assertEquals(true, StringUtils.contains("abc", 'a'));
assertEquals(true, StringUtils.contains("abc", 'b'));
assertEquals(true, StringUtils.contains("abc", 'c'));
assertEquals(false, StringUtils.contains("abc", 'z'));
}
public void contains_String() {
public void testContainsString() {
assertEquals(false, StringUtils.contains(null, null));
assertEquals(false, StringUtils.contains(null, ""));
assertEquals(false, StringUtils.contains(null, "a"));
@ -246,7 +256,15 @@ public void testIndexOfAny() {
assertEquals(-1, StringUtils.indexOfAny(FOOBAR, null));
assertEquals(2, StringUtils.indexOfAny(FOOBAR, FOOBAR_SUB_ARRAY));
assertEquals(-1, StringUtils.indexOfAny(FOOBAR, new String[0]));
assertEquals(-1, StringUtils.indexOfAny(null, new String[0]));
assertEquals(-1, StringUtils.indexOfAny("", new String[0]));
assertEquals(-1, StringUtils.indexOfAny(FOOBAR, new String[] {"llll"}));
assertEquals(0, StringUtils.indexOfAny(FOOBAR, new String[] {""}));
assertEquals(0, StringUtils.indexOfAny("", new String[] {""}));
assertEquals(-1, StringUtils.indexOfAny("", new String[] {"a"}));
assertEquals(-1, StringUtils.indexOfAny("", new String[] {null}));
assertEquals(-1, StringUtils.indexOfAny(FOOBAR, new String[] {null}));
assertEquals(-1, StringUtils.indexOfAny(null, new String[] {null}));
}
public void testLastIndexOfAny() {
@ -255,7 +273,15 @@ public void testLastIndexOfAny() {
assertEquals(-1, StringUtils.lastIndexOfAny(FOOBAR, null));
assertEquals(3, StringUtils.lastIndexOfAny(FOOBAR, FOOBAR_SUB_ARRAY));
assertEquals(-1, StringUtils.lastIndexOfAny(FOOBAR, new String[0]));
assertEquals(-1, StringUtils.lastIndexOfAny(null, new String[0]));
assertEquals(-1, StringUtils.lastIndexOfAny("", new String[0]));
assertEquals(-1, StringUtils.lastIndexOfAny(FOOBAR, new String[] {"llll"}));
assertEquals(6, StringUtils.lastIndexOfAny(FOOBAR, new String[] {""}));
assertEquals(0, StringUtils.lastIndexOfAny("", new String[] {""}));
assertEquals(-1, StringUtils.lastIndexOfAny("", new String[] {"a"}));
assertEquals(-1, StringUtils.lastIndexOfAny("", new String[] {null}));
assertEquals(-1, StringUtils.lastIndexOfAny(FOOBAR, new String[] {null}));
assertEquals(-1, StringUtils.lastIndexOfAny(null, new String[] {null}));
}
//-----------------------------------------------------------------------
@ -281,6 +307,18 @@ public void testIndexOfAnyBut() {
assertEquals(0, StringUtils.indexOfAnyBut(str3, chars1));
assertEquals(1, StringUtils.indexOfAnyBut(str3, chars2));
assertEquals(-1, StringUtils.indexOfAnyBut(str3, chars3));
assertEquals(3, StringUtils.indexOfAnyBut("zzabyycdxx", "za"));
assertEquals(0, StringUtils.indexOfAnyBut("zzabyycdxx", ""));
assertEquals(-1, StringUtils.indexOfAnyBut("aba","ab"));
}
//-----------------------------------------------------------------------
public void testIndexOfAnyButChar() {
assertEquals(-1, StringUtils.indexOfAnyBut(null, new char[0]));
assertEquals(-1, StringUtils.indexOfAnyBut("", new char[0]));
assertEquals(3, StringUtils.indexOfAnyBut("zzabyycdxx", new char[] {'z','a'}));
assertEquals(0, StringUtils.indexOfAnyBut("zzabyycdxx", new char[0]));
assertEquals(-1, StringUtils.indexOfAnyBut("ab", new char[] {'a','b'}));
}
//-----------------------------------------------------------------------

View File

@ -63,7 +63,8 @@
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: StringUtilsSubstringTest.java,v 1.6 2003/07/20 15:29:44 scolebourne Exp $
* @author Phil Steitz
* @version $Id: StringUtilsSubstringTest.java,v 1.7 2003/07/20 23:57:26 scolebourne Exp $
*/
public class StringUtilsSubstringTest extends TestCase {
private static final String FOO = "foo";
@ -111,6 +112,19 @@ public void testSubstring3() {
assertEquals(FOO, StringUtils.substring(SENTENCE, 0, -8));
assertEquals("o", StringUtils.substring(SENTENCE, -9, -8));
assertEquals(SENTENCE, StringUtils.substring(SENTENCE, 0, 80));
assertEquals("", StringUtils.substring(SENTENCE, 2, 2));
assertEquals("b",StringUtils.substring("abc", -2, -1));
}
public void testSubstring4() {
assertEquals("", StringUtils.substring("",0));
assertEquals("", StringUtils.substring("",2));
assertEquals("", StringUtils.substring("",0,0));
assertEquals("", StringUtils.substring("",1,2));
assertEquals("", StringUtils.substring("",-2,-1));
assertEquals(null, StringUtils.substring(null,0));
assertEquals(null, StringUtils.substring(null,0,0));
assertEquals(null, StringUtils.substring(null,1,2));
}
public void testLeft() {

View File

@ -71,7 +71,8 @@
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
* @author Holger Krauth
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @version $Id: StringUtilsTest.java,v 1.35 2003/07/20 15:49:58 scolebourne Exp $
* @author Phil Steitz
* @version $Id: StringUtilsTest.java,v 1.36 2003/07/20 23:57:26 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
@ -276,6 +277,13 @@ public void testSplit_StringChar() {
res = StringUtils.split(str, '.');
assertEquals(1, res.length);
assertEquals("a", res[0]);
str = "a b c";
res = StringUtils.split(str,' ');
assertEquals(3, res.length);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals("c", res[2]);
}
public void testSplit_StringString_StringStringInt() {