Add new methods for null-safe indexOf/lastIndexOf
Add new methods for contains git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137461 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9ea271c0c7
commit
51af2aef0b
|
@ -98,7 +98,7 @@ import org.apache.commons.lang.math.NumberUtils;
|
|||
* @author Arun Mammen Thomas
|
||||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||
* @since 1.0
|
||||
* @version $Id: StringUtils.java,v 1.66 2003/07/19 21:55:05 scolebourne Exp $
|
||||
* @version $Id: StringUtils.java,v 1.67 2003/07/19 23:29:06 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
||||
|
@ -464,6 +464,310 @@ public class StringUtils {
|
|||
// IndexOf
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Finds the first index within a String, handling <code>null</code>.
|
||||
* This method uses {@link String#indexOf(int)}</p>
|
||||
*
|
||||
* <p>A <code>null</code> String will return <code>-1</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.indexOf(null, ' ') = -1
|
||||
* StringUtils.indexOf("", ' ') = -1
|
||||
* StringUtils.indexOf("aabaabaa", 'a') = 0
|
||||
* StringUtils.indexOf("aabaabaa", 'b') = 2
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchChar the character to find
|
||||
* @return the first index of the search character,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int indexOf(String str, char searchChar) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return str.indexOf(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>
|
||||
*
|
||||
* <p>A <code>null</code> 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("aabaabaa", 'b', 0) = 2
|
||||
* StringUtils.indexOf("aabaabaa", 'b', 3) = 5
|
||||
* StringUtils.indexOf("aabaabaa", 'b', 9) = -1
|
||||
* StringUtils.indexOf("aabaabaa", 'b', -1) = 2
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchChar the character to find
|
||||
* @param startPos the start position, negative treated as zero
|
||||
* @return the first index of the search character,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int indexOf(String str, char searchChar, int startPos) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return str.indexOf(searchChar, startPos);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Finds the first index within a String, handling <code>null</code>.
|
||||
* 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("", "") = 0
|
||||
* StringUtils.indexOf("aabaabaa", "a") = 0
|
||||
* StringUtils.indexOf("aabaabaa", "b") = 2
|
||||
* StringUtils.indexOf("aabaabaa", "ab") = 1
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchStr the String to find, may be null
|
||||
* @return the first index of the search String,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int indexOf(String str, String searchStr) {
|
||||
if (str == null || searchStr == null) {
|
||||
return -1;
|
||||
}
|
||||
return str.indexOf(searchStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Finds the first index within a String, handling <code>null</code>.
|
||||
* 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.
|
||||
* An empty ("") search String always matches.
|
||||
* A start position greater than the string length only matches
|
||||
* 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("", "", 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
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchStr the String to find, may be null
|
||||
* @param startPos the start position, negative treated as zero
|
||||
* @return the first index of the search String,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int indexOf(String str, String searchStr, int startPos) {
|
||||
if (str == null || searchStr == null) {
|
||||
return -1;
|
||||
}
|
||||
return str.indexOf(searchStr, startPos);
|
||||
}
|
||||
|
||||
// LastIndexOf
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Finds the last index within a String, handling <code>null</code>.
|
||||
* This method uses {@link String#indexOf(int)}</p>
|
||||
*
|
||||
* <p>A <code>null</code> String will return <code>-1</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.lastIndexOf(null, ' ') = -1
|
||||
* StringUtils.lastIndexOf("", ' ') = -1
|
||||
* StringUtils.lastIndexOf("aabaabaa", 'a') = 7
|
||||
* StringUtils.lastIndexOf("aabaabaa", 'b') = 5
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchChar the character to find
|
||||
* @return the last index of the search character,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int lastIndexOf(String str, char searchChar) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return str.lastIndexOf(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>
|
||||
*
|
||||
* <p>A <code>null</code> String will return <code>-1</code>.
|
||||
* A negative or zero 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("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
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchChar the character to find
|
||||
* @param startPos the start position
|
||||
* @return the last index of the search character,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int lastIndexOf(String str, char searchChar, int startPos) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return str.lastIndexOf(searchChar, startPos);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Finds the last index within a String, handling <code>null</code>.
|
||||
* This method uses {@link String#indexOf(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("", "") = 0
|
||||
* StringUtils.lastIndexOf("aabaabaa", "a") = 0
|
||||
* StringUtils.lastIndexOf("aabaabaa", "b") = 2
|
||||
* StringUtils.lastIndexOf("aabaabaa", "ab") = 1
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchStr the String to find, may be null
|
||||
* @return the last index of the search String,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int lastIndexOf(String str, String searchStr) {
|
||||
if (str == null || searchStr == null) {
|
||||
return -1;
|
||||
}
|
||||
return str.lastIndexOf(searchStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Finds the first index within a String, handling <code>null</code>.
|
||||
* This method uses {@link String#indexOf(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.
|
||||
* 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("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
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchStr the String to find, may be null
|
||||
* @param startPos the start position, negative treated as zero
|
||||
* @return the first index of the search String,
|
||||
* -1 if no match or <code>null</code> string input
|
||||
*/
|
||||
public static int lastIndexOf(String str, String searchStr, int startPos) {
|
||||
if (str == null || searchStr == null) {
|
||||
return -1;
|
||||
}
|
||||
return str.lastIndexOf(searchStr, startPos);
|
||||
}
|
||||
|
||||
// Contains
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Checks if String contains a search character, handling <code>null</code>.
|
||||
* This method uses {@link String#indexOf(int)}</p>
|
||||
*
|
||||
* <p>A <code>null</code> String will return <code>-1</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.contains(null, ' ') = false
|
||||
* StringUtils.contains("", ' ') = false
|
||||
* StringUtils.contains("abc", 'a') = true
|
||||
* StringUtils.contains("abc", 'z') = false
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchChar the character to find
|
||||
* @return true if the String contains the search character,
|
||||
* false if not or <code>null</code> string input
|
||||
*/
|
||||
public static boolean contains(String str, char searchChar) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
return (str.indexOf(searchChar) >= 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Find the first index within a String, handling <code>null</code>.
|
||||
* This method uses {@link String#indexOf(String)}</p>
|
||||
*
|
||||
* <p>A <code>null</code> String will return <code>-1</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.contains(null, null) = false
|
||||
* StringUtils.contains("", null) = false
|
||||
* StringUtils.contains("", "") = true
|
||||
* StringUtils.contains("abc", "") = true
|
||||
* StringUtils.contains("abc", "a") = true
|
||||
* StringUtils.contains("abc", "z") = false
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchStr the String to find, may be null
|
||||
* @return true if the String contains the search character,
|
||||
* false if not or <code>null</code> string input
|
||||
*/
|
||||
public static boolean contains(String str, String searchStr) {
|
||||
if (str == null || searchStr == null) {
|
||||
return false;
|
||||
}
|
||||
return (str.indexOf(searchStr) >= 0);
|
||||
}
|
||||
|
||||
// IndexOfAny
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Find the first index of any of a set of potential substrings.</p>
|
||||
*
|
||||
|
@ -554,6 +858,9 @@ public class StringUtils {
|
|||
return ret;
|
||||
}
|
||||
|
||||
// IndexOfAnyBut
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Search a String to find the first index of any
|
||||
* character not in the given set of characters.</p>
|
||||
|
@ -591,7 +898,7 @@ public class StringUtils {
|
|||
return -1;
|
||||
}
|
||||
|
||||
// Contains
|
||||
// ContainsOnly
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
@ -651,6 +958,9 @@ public class StringUtils {
|
|||
return containsOnly(str, validChars.toCharArray());
|
||||
}
|
||||
|
||||
// ContainsNone
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Checks that the String does not contain certain characters.</p>
|
||||
*
|
||||
|
|
|
@ -63,7 +63,7 @@ import junit.textui.TestRunner;
|
|||
*
|
||||
* @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.2 2003/03/23 21:50:58 scolebourne Exp $
|
||||
* @version $Id: StringUtilsEqualsIndexOfTest.java,v 1.3 2003/07/19 23:29:06 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtilsEqualsIndexOfTest extends TestCase {
|
||||
private static final String FOO = "foo";
|
||||
|
@ -115,6 +115,131 @@ public class StringUtilsEqualsIndexOfTest extends TestCase {
|
|||
assertEquals(false, StringUtils.equalsIgnoreCase(null, FOO));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testIndexOf_char() {
|
||||
assertEquals(-1, StringUtils.indexOf(null, ' '));
|
||||
assertEquals(-1, StringUtils.indexOf("", ' '));
|
||||
assertEquals(0, StringUtils.indexOf("aabaabaa", 'a'));
|
||||
assertEquals(2, StringUtils.indexOf("aabaabaa", 'b'));
|
||||
}
|
||||
|
||||
public void testIndexOf_charInt() {
|
||||
assertEquals(-1, StringUtils.indexOf(null, ' ', 0));
|
||||
assertEquals(-1, StringUtils.indexOf(null, ' ', -1));
|
||||
assertEquals(-1, StringUtils.indexOf("", ' ', 0));
|
||||
assertEquals(-1, StringUtils.indexOf("", ' ', -1));
|
||||
assertEquals(0, StringUtils.indexOf("aabaabaa", 'a', 0));
|
||||
assertEquals(2, StringUtils.indexOf("aabaabaa", 'b', 0));
|
||||
assertEquals(5, StringUtils.indexOf("aabaabaa", 'b', 3));
|
||||
assertEquals(-1, StringUtils.indexOf("aabaabaa", 'b', 9));
|
||||
assertEquals(2, StringUtils.indexOf("aabaabaa", 'b', -1));
|
||||
}
|
||||
|
||||
public void testIndexOf_String() {
|
||||
assertEquals(-1, StringUtils.indexOf(null, null));
|
||||
assertEquals(-1, StringUtils.indexOf("", null));
|
||||
assertEquals(0, StringUtils.indexOf("", ""));
|
||||
assertEquals(0, StringUtils.indexOf("aabaabaa", "a"));
|
||||
assertEquals(2, StringUtils.indexOf("aabaabaa", "b"));
|
||||
assertEquals(1, StringUtils.indexOf("aabaabaa", "ab"));
|
||||
}
|
||||
|
||||
public void testIndexOf_StringInt() {
|
||||
assertEquals(-1, StringUtils.indexOf(null, null, 0));
|
||||
assertEquals(-1, StringUtils.indexOf(null, null, -1));
|
||||
assertEquals(-1, StringUtils.indexOf(null, "", 0));
|
||||
assertEquals(-1, StringUtils.indexOf(null, "", -1));
|
||||
assertEquals(-1, StringUtils.indexOf("", null, 0));
|
||||
assertEquals(-1, StringUtils.indexOf("", null, -1));
|
||||
assertEquals(0, StringUtils.indexOf("", "", 0));
|
||||
assertEquals(0, StringUtils.indexOf("", "", -1));
|
||||
assertEquals(0, StringUtils.indexOf("", "", 9));
|
||||
assertEquals(0, StringUtils.indexOf("abc", "", 0));
|
||||
assertEquals(0, StringUtils.indexOf("abc", "", -1));
|
||||
assertEquals(3, StringUtils.indexOf("abc", "", 9));
|
||||
assertEquals(0, StringUtils.indexOf("aabaabaa", "a", 0));
|
||||
assertEquals(2, StringUtils.indexOf("aabaabaa", "b", 0));
|
||||
assertEquals(1, StringUtils.indexOf("aabaabaa", "ab", 0));
|
||||
assertEquals(5, StringUtils.indexOf("aabaabaa", "b", 3));
|
||||
assertEquals(-1, StringUtils.indexOf("aabaabaa", "b", 9));
|
||||
assertEquals(2, StringUtils.indexOf("aabaabaa", "b", -1));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testLastIndexOf_char() {
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, ' '));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("", ' '));
|
||||
assertEquals(7, StringUtils.lastIndexOf("aabaabaa", 'a'));
|
||||
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", 'b'));
|
||||
}
|
||||
|
||||
public void testLastIndexOf_charInt() {
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, ' ', 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, ' ', -1));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("", ' ', 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("", ' ', -1));
|
||||
assertEquals(7, StringUtils.lastIndexOf("aabaabaa", 'a', 8));
|
||||
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", 'b', 8));
|
||||
assertEquals(2, StringUtils.lastIndexOf("aabaabaa", 'b', 3));
|
||||
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", 'b', 9));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", 'b', -1));
|
||||
}
|
||||
|
||||
public void testLastIndexOf_String() {
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, null));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("", null));
|
||||
assertEquals(0, StringUtils.lastIndexOf("", ""));
|
||||
assertEquals(7, StringUtils.lastIndexOf("aabaabaa", "a"));
|
||||
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b"));
|
||||
assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab"));
|
||||
}
|
||||
|
||||
public void testLastIndexOf_StringInt() {
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, null, 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, null, -1));
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, "", 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, "", -1));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("", null, 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("", null, -1));
|
||||
assertEquals(0, StringUtils.lastIndexOf("", "", 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("", "", -1));
|
||||
assertEquals(0, StringUtils.lastIndexOf("", "", 9));
|
||||
assertEquals(0, StringUtils.lastIndexOf("abc", "", 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("abc", "", -1));
|
||||
assertEquals(3, StringUtils.lastIndexOf("abc", "", 9));
|
||||
assertEquals(7, StringUtils.lastIndexOf("aabaabaa", "a", 8));
|
||||
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b", 8));
|
||||
assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab", 8));
|
||||
assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "b", 3));
|
||||
assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b", 9));
|
||||
assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", -1));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void contains_char() {
|
||||
assertEquals(false, StringUtils.contains(null, ' '));
|
||||
assertEquals(false, StringUtils.contains("", ' '));
|
||||
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() {
|
||||
assertEquals(false, StringUtils.contains(null, null));
|
||||
assertEquals(false, StringUtils.contains(null, ""));
|
||||
assertEquals(false, StringUtils.contains(null, "a"));
|
||||
assertEquals(false, StringUtils.contains("", null));
|
||||
assertEquals(true, StringUtils.contains("", ""));
|
||||
assertEquals(false, StringUtils.contains("", "a"));
|
||||
assertEquals(true, StringUtils.contains("abc", "a"));
|
||||
assertEquals(true, StringUtils.contains("abc", "b"));
|
||||
assertEquals(true, StringUtils.contains("abc", "c"));
|
||||
assertEquals(true, StringUtils.contains("abc", "abc"));
|
||||
assertEquals(false, StringUtils.contains("abc", "z"));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testIndexOfAny() {
|
||||
assertEquals(-1, StringUtils.indexOfAny(null, null));
|
||||
assertEquals(-1, StringUtils.indexOfAny(null, FOOBAR_SUB_ARRAY));
|
||||
|
@ -133,4 +258,128 @@ public class StringUtilsEqualsIndexOfTest extends TestCase {
|
|||
assertEquals(-1, StringUtils.lastIndexOfAny(FOOBAR, new String[] {"llll"}));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testIndexOfAnyBut() {
|
||||
String str1 = "a";
|
||||
String str2 = "b";
|
||||
String str3 = "ab";
|
||||
String chars1= "b";
|
||||
String chars2= "a";
|
||||
String chars3= "ab";
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut(null, (String) null));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut("", (String) null));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut(null, ""));
|
||||
assertEquals(0, StringUtils.indexOfAnyBut(str1, ""));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut("", ""));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut("", chars1));
|
||||
assertEquals(0, StringUtils.indexOfAnyBut(str1, chars1));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut(str1, chars2));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut(str1, chars3));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut(str2, chars1));
|
||||
assertEquals(0, StringUtils.indexOfAnyBut(str2, chars2));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut(str2, chars3));
|
||||
assertEquals(0, StringUtils.indexOfAnyBut(str3, chars1));
|
||||
assertEquals(1, StringUtils.indexOfAnyBut(str3, chars2));
|
||||
assertEquals(-1, StringUtils.indexOfAnyBut(str3, chars3));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testContainsOnly_String() {
|
||||
String str1 = "a";
|
||||
String str2 = "b";
|
||||
String str3 = "ab";
|
||||
String chars1= "b";
|
||||
String chars2= "a";
|
||||
String chars3= "ab";
|
||||
assertEquals(false, StringUtils.containsOnly(null, (String) null));
|
||||
assertEquals(false, StringUtils.containsOnly("", (String) null));
|
||||
assertEquals(false, StringUtils.containsOnly(null, ""));
|
||||
assertEquals(false, StringUtils.containsOnly(str1, ""));
|
||||
assertEquals(true, StringUtils.containsOnly("", ""));
|
||||
assertEquals(true, StringUtils.containsOnly("", chars1));
|
||||
assertEquals(false, StringUtils.containsOnly(str1, chars1));
|
||||
assertEquals(true, StringUtils.containsOnly(str1, chars2));
|
||||
assertEquals(true, StringUtils.containsOnly(str1, chars3));
|
||||
assertEquals(true, StringUtils.containsOnly(str2, chars1));
|
||||
assertEquals(false, StringUtils.containsOnly(str2, chars2));
|
||||
assertEquals(true, StringUtils.containsOnly(str2, chars3));
|
||||
assertEquals(false, StringUtils.containsOnly(str3, chars1));
|
||||
assertEquals(false, StringUtils.containsOnly(str3, chars2));
|
||||
assertEquals(true, StringUtils.containsOnly(str3, chars3));
|
||||
}
|
||||
|
||||
public void testContainsOnly_CharArray() {
|
||||
String str1 = "a";
|
||||
String str2 = "b";
|
||||
String str3 = "ab";
|
||||
char[] chars1= {'b'};
|
||||
char[] chars2= {'a'};
|
||||
char[] chars3= {'a', 'b'};
|
||||
char[] emptyChars = new char[0];
|
||||
assertEquals(false, StringUtils.containsOnly(null, (char[]) null));
|
||||
assertEquals(false, StringUtils.containsOnly("", (char[]) null));
|
||||
assertEquals(false, StringUtils.containsOnly(null, emptyChars));
|
||||
assertEquals(false, StringUtils.containsOnly(str1, emptyChars));
|
||||
assertEquals(true, StringUtils.containsOnly("", emptyChars));
|
||||
assertEquals(true, StringUtils.containsOnly("", chars1));
|
||||
assertEquals(false, StringUtils.containsOnly(str1, chars1));
|
||||
assertEquals(true, StringUtils.containsOnly(str1, chars2));
|
||||
assertEquals(true, StringUtils.containsOnly(str1, chars3));
|
||||
assertEquals(true, StringUtils.containsOnly(str2, chars1));
|
||||
assertEquals(false, StringUtils.containsOnly(str2, chars2));
|
||||
assertEquals(true, StringUtils.containsOnly(str2, chars3));
|
||||
assertEquals(false, StringUtils.containsOnly(str3, chars1));
|
||||
assertEquals(false, StringUtils.containsOnly(str3, chars2));
|
||||
assertEquals(true, StringUtils.containsOnly(str3, chars3));
|
||||
}
|
||||
|
||||
public void testContainsNone_String() {
|
||||
String str1 = "a";
|
||||
String str2 = "b";
|
||||
String str3 = "ab.";
|
||||
String chars1= "b";
|
||||
String chars2= ".";
|
||||
String chars3= "cd";
|
||||
assertEquals(true, StringUtils.containsNone(null, (String) null));
|
||||
assertEquals(true, StringUtils.containsNone("", (String) null));
|
||||
assertEquals(true, StringUtils.containsNone(null, ""));
|
||||
assertEquals(true, StringUtils.containsNone(str1, ""));
|
||||
assertEquals(true, StringUtils.containsNone("", ""));
|
||||
assertEquals(true, StringUtils.containsNone("", chars1));
|
||||
assertEquals(true, StringUtils.containsNone(str1, chars1));
|
||||
assertEquals(true, StringUtils.containsNone(str1, chars2));
|
||||
assertEquals(true, StringUtils.containsNone(str1, chars3));
|
||||
assertEquals(false, StringUtils.containsNone(str2, chars1));
|
||||
assertEquals(true, StringUtils.containsNone(str2, chars2));
|
||||
assertEquals(true, StringUtils.containsNone(str2, chars3));
|
||||
assertEquals(false, StringUtils.containsNone(str3, chars1));
|
||||
assertEquals(false, StringUtils.containsNone(str3, chars2));
|
||||
assertEquals(true, StringUtils.containsNone(str3, chars3));
|
||||
}
|
||||
|
||||
public void testContainsNone_CharArray() {
|
||||
String str1 = "a";
|
||||
String str2 = "b";
|
||||
String str3 = "ab.";
|
||||
char[] chars1= {'b'};
|
||||
char[] chars2= {'.'};
|
||||
char[] chars3= {'c', 'd'};
|
||||
char[] emptyChars = new char[0];
|
||||
assertEquals(true, StringUtils.containsNone(null, (char[]) null));
|
||||
assertEquals(true, StringUtils.containsNone("", (char[]) null));
|
||||
assertEquals(true, StringUtils.containsNone(null, emptyChars));
|
||||
assertEquals(true, StringUtils.containsNone(str1, emptyChars));
|
||||
assertEquals(true, StringUtils.containsNone("", emptyChars));
|
||||
assertEquals(true, StringUtils.containsNone("", chars1));
|
||||
assertEquals(true, StringUtils.containsNone(str1, chars1));
|
||||
assertEquals(true, StringUtils.containsNone(str1, chars2));
|
||||
assertEquals(true, StringUtils.containsNone(str1, chars3));
|
||||
assertEquals(false, StringUtils.containsNone(str2, chars1));
|
||||
assertEquals(true, StringUtils.containsNone(str2, chars2));
|
||||
assertEquals(true, StringUtils.containsNone(str2, chars3));
|
||||
assertEquals(false, StringUtils.containsNone(str3, chars1));
|
||||
assertEquals(false, StringUtils.containsNone(str3, chars2));
|
||||
assertEquals(true, StringUtils.containsNone(str3, chars3));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue