mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-13 05:25:20 +00:00
Update slice methods to be more consistent
from Phil Steitz git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137492 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e758deb5e8
commit
d33605dfc9
@ -146,7 +146,7 @@
|
||||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||
* @author Phil Steitz
|
||||
* @since 1.0
|
||||
* @version $Id: StringUtils.java,v 1.77 2003/07/22 23:36:40 scolebourne Exp $
|
||||
* @version $Id: StringUtils.java,v 1.78 2003/07/25 00:50:00 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
||||
@ -2438,8 +2438,6 @@ public static String slice(String str) {
|
||||
* <p>A <code>null</code> string input will return <code>null</code>.
|
||||
* An empty ("") string input will return the empty string.
|
||||
* An empty or <code>null</code> separator will return the input string.</p>
|
||||
*
|
||||
* <p>This method is the opposite of {@link #sliceRemainder(String, String)}.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.slice(null, *) = null
|
||||
@ -2462,12 +2460,11 @@ public static String slice(String str, String separator) {
|
||||
if (str == null || separator == null || str.length() == 0 || separator.length() == 0) {
|
||||
return str;
|
||||
}
|
||||
int idx = str.lastIndexOf(separator);
|
||||
if (idx != -1) {
|
||||
return str.substring(0, idx);
|
||||
} else {
|
||||
int pos = str.lastIndexOf(separator);
|
||||
if (pos == -1) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2476,9 +2473,8 @@ public static String slice(String str, String separator) {
|
||||
*
|
||||
* <p>A <code>null</code> string input will return <code>null</code>.
|
||||
* An empty ("") string input will return the empty string.
|
||||
* An empty or <code>null</code> separator will return the empty string.</p>
|
||||
*
|
||||
* <p>This method is the opposite of {@link #slice(String, String)}.</p>
|
||||
* An empty or <code>null</code> separator will return the empty string if
|
||||
* the input string is not <code>null</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.sliceRemainder(null, *) = null
|
||||
@ -2506,14 +2502,11 @@ public static String sliceRemainder(String str, String separator) {
|
||||
if (separator == null || separator.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
int idx = str.lastIndexOf(separator);
|
||||
if (idx == str.length() - separator.length()) {
|
||||
return "";
|
||||
} else if (idx != -1) {
|
||||
return str.substring(idx + separator.length());
|
||||
} else {
|
||||
int pos = str.lastIndexOf(separator);
|
||||
if (pos == -1 || pos == (str.length() - separator.length())) {
|
||||
return "";
|
||||
}
|
||||
return str.substring(pos + separator.length());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2522,9 +2515,7 @@ public static String sliceRemainder(String str, String separator) {
|
||||
*
|
||||
* <p>A <code>null</code> string input will return <code>null</code>.
|
||||
* An empty ("") string input will return the empty string.
|
||||
* An empty or <code>null</code> separator will return the input string.</p>
|
||||
*
|
||||
* <p>This method is the opposite of {@link #sliceFirst(String, String)}.</p>
|
||||
* A <code>null</code> separator will return the input string.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.sliceFirst(null, *) = null
|
||||
@ -2532,8 +2523,8 @@ public static String sliceRemainder(String str, String separator) {
|
||||
* StringUtils.sliceFirst("abc", "a") = ""
|
||||
* StringUtils.sliceFirst("abcba", "b") = "a"
|
||||
* StringUtils.sliceFirst("abc", "c") = "ab"
|
||||
* StringUtils.sliceFirst("abc", "d") = ""
|
||||
* StringUtils.sliceFirst("abc", "") = "abc"
|
||||
* StringUtils.sliceFirst("abc", "d") = "abc"
|
||||
* StringUtils.sliceFirst("abc", "") = ""
|
||||
* StringUtils.sliceFirst("abc", null) = "abc"
|
||||
* </pre>
|
||||
*
|
||||
@ -2545,15 +2536,17 @@ public static String sliceRemainder(String str, String separator) {
|
||||
* @return sliced String, <code>null</code> if null String input
|
||||
*/
|
||||
public static String sliceFirst(String str, String separator) {
|
||||
if (str == null || separator == null || str.length() == 0 || separator.length() == 0) {
|
||||
if (str == null || separator == null || str.length() == 0) {
|
||||
return str;
|
||||
}
|
||||
int idx = str.indexOf(separator);
|
||||
if (idx != -1) {
|
||||
return str.substring(0, idx);
|
||||
} else {
|
||||
if (separator.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
int pos = str.indexOf(separator);
|
||||
if (pos == -1) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2562,19 +2555,18 @@ public static String sliceFirst(String str, String separator) {
|
||||
*
|
||||
* <p>A <code>null</code> string input will return <code>null</code>.
|
||||
* An empty ("") string input will return the empty string.
|
||||
* An empty or <code>null</code> separator will return the empty string.</p>
|
||||
* A <code>null</code> separator will return the empty string if the
|
||||
* input string is not <code>null</code>.</p>
|
||||
*
|
||||
* <p>This method is the opposite of {@link #sliceFirst(String, String)}.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.sliceFirstRemainder(null, *) = null
|
||||
* StringUtils.sliceFirstRemainder("", *) = ""
|
||||
* StringUtils.sliceFirstRemainder(*, "") = ""
|
||||
* StringUtils.sliceFirstRemainder(*, null) = ""
|
||||
* StringUtils.sliceFirstRemainder("abc", "a") = "bc"
|
||||
* StringUtils.sliceFirstRemainder("abcba", "b") = "cba"
|
||||
* StringUtils.sliceFirstRemainder("abc", "c") = ""
|
||||
* StringUtils.sliceFirstRemainder("abc", "d") = "abc"
|
||||
* StringUtils.sliceFirstRemainder(null, *) = null
|
||||
* StringUtils.sliceFirstRemainder("", *) = ""
|
||||
* StringUtils.sliceFirstRemainder(*, null) = ""
|
||||
* StringUtils.sliceFirstRemainder("abc", "a") = "bc"
|
||||
* StringUtils.sliceFirstRemainder("abcba", "b") = "cba"
|
||||
* StringUtils.sliceFirstRemainder("abc", "c") = ""
|
||||
* StringUtils.sliceFirstRemainder("abc", "d") = ""
|
||||
* StringUtils.sliceFirstRemainder("abc", "") = "abc"
|
||||
* </pre>
|
||||
*
|
||||
* <p><em>(This method was formerly named prechomp. Also, previously
|
||||
@ -2588,15 +2580,14 @@ public static String sliceFirstRemainder(String str, String separator) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return str;
|
||||
}
|
||||
if (separator == null || separator.length() == 0) {
|
||||
if (separator == null) {
|
||||
return "";
|
||||
}
|
||||
int idx = str.indexOf(separator);
|
||||
if (idx != -1) {
|
||||
return str.substring(idx + separator.length());
|
||||
} else {
|
||||
return str;
|
||||
int pos = str.indexOf(separator);
|
||||
if (pos == -1) {
|
||||
return "";
|
||||
}
|
||||
return str.substring(pos + separator.length());
|
||||
}
|
||||
|
||||
// Conversion
|
||||
|
@ -72,7 +72,7 @@
|
||||
* @author Holger Krauth
|
||||
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
|
||||
* @author Phil Steitz
|
||||
* @version $Id: StringUtilsTest.java,v 1.37 2003/07/22 23:36:39 scolebourne Exp $
|
||||
* @version $Id: StringUtilsTest.java,v 1.38 2003/07/25 00:50:00 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtilsTest extends TestCase {
|
||||
|
||||
@ -649,13 +649,12 @@ public void testSliceFirst_StringString() {
|
||||
assertEquals("", StringUtils.sliceFirst("", "XX"));
|
||||
|
||||
assertEquals("foo", StringUtils.sliceFirst("foo", null));
|
||||
assertEquals("", StringUtils.sliceFirst("foo", "b"));
|
||||
assertEquals("foo", StringUtils.sliceFirst("foo", "b"));
|
||||
assertEquals("f", StringUtils.sliceFirst("foot", "o"));
|
||||
assertEquals("", StringUtils.sliceFirst("abc", "a"));
|
||||
assertEquals("a", StringUtils.sliceFirst("abcba", "b"));
|
||||
assertEquals("ab", StringUtils.sliceFirst("abc", "c"));
|
||||
assertEquals("abc", StringUtils.sliceFirst("abc", ""));
|
||||
assertEquals("", StringUtils.sliceFirst("abc", "d"));
|
||||
assertEquals("", StringUtils.sliceFirst("abc", ""));
|
||||
}
|
||||
|
||||
public void testSliceFirstRemainder_StringString() {
|
||||
@ -669,13 +668,12 @@ public void testSliceFirstRemainder_StringString() {
|
||||
assertEquals("", StringUtils.sliceFirstRemainder("", "XX"));
|
||||
|
||||
assertEquals("", StringUtils.sliceFirstRemainder("foo", null));
|
||||
assertEquals("foo", StringUtils.sliceFirstRemainder("foo", "b"));
|
||||
assertEquals("ot", StringUtils.sliceFirstRemainder("foot", "o"));
|
||||
assertEquals("bc", StringUtils.sliceFirstRemainder("abc", "a"));
|
||||
assertEquals("cba", StringUtils.sliceFirstRemainder("abcba", "b"));
|
||||
assertEquals("", StringUtils.sliceFirstRemainder("abc", "c"));
|
||||
assertEquals("", StringUtils.sliceFirstRemainder("abc", ""));
|
||||
assertEquals("abc", StringUtils.sliceFirstRemainder("abc", "d"));
|
||||
assertEquals("abc", StringUtils.sliceFirstRemainder("abc", ""));
|
||||
assertEquals("", StringUtils.sliceFirstRemainder("abc", "d"));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user