A DESCRIPTION OF THE FIX :
RFE: Add endsWithIgnoreCase(String) public method to java.lang.String class.
DESCRIPTION:
The String class has no public method to test a string suffix ignoring case considerations. The regionMatches(boolean, int, String, int, int) can be used to do that but it takes four parameters.
It would be helpful for Java developers to have endsWithIgnoreCase(String) method the
same way equals(String) and compareTo(String) methods have equalsIgnoreCase(String) and compareToIgnoreCase(String) respectively which take only one parameter.
DIFF BASELINE: jdk-6_0-ea-src-b45-jrl-28_jul_2005
DIFF:
--- src/java/lang/String.java.orig 2005-07-28 00:51:00.000000000 +0100
+++ src/java/lang/String.java 2005-08-04 12:52:46.000000000 +0100
@@ -1303,7 +1303,26 @@
public boolean endsWith(String suffix) {
return startsWith(suffix, count - suffix.count);
}
-
+
+ /**
+ * Tests if this string ends with the specified suffix, ignoring case
+ * considerations.
+ *
+ * @param suffix the suffix.
+ * @return <code>true</code> if the character sequence represented by the
+ * argument is a suffix of the character sequence represented by
+ * this object ignoring case considerations; <code>false</code> otherwise.
+ * Note that the result will be <code>true</code> if the argument is the
+ * empty string or is equal to this <code>String</code> object
+ * as determined by the {@link #equals(Object)} method.
+ * @since 1. 6
+ */
+ public boolean endsWithIgnoreCase(String suffix) {
+ return (count >= suffix.count)
+ ? regionMatches(true, count - suffix.count, suffix, 0, suffix.count)
+ : false;
+ }
+
/**
* Returns a hash code for this string. The hash code for a
* <code>String</code> object is computed as
@@ -2763,3 +2782,4 @@
public native String intern();
}
+
JUnit TESTCASE :
This Junit test case shows endsWith(String) method is case sensitive and then test
endsWithIgnoreCase(String).
Note that I did not change endsWith(String) implementation, I only added endsWithIgnoreCase(String) and its implementation to java.lang.String class.
----------------------------------------------------------------------
import junit.framework.*;
public class StringTest extends TestCase {
boolean actualResult;
String name = "file.java";
String suffix0 = ".java";
String suffix1 = ".JaVa";
String suffix2 = "file.java";
String suffix3 = "abcfile.java";
String suffix4 = " ";
String suffix5 = "";
public StringTest(String testName) {
super(testName);
}
public void testEndsWithIgnoreCase() {
actualResult = name.endsWithIgnoreCase(suffix0);
assertTrue(actualResult);
actualResult = name.endsWithIgnoreCase(suffix1);
assertTrue(actualResult);
actualResult = name.endsWithIgnoreCase(suffix2);
assertTrue(actualResult);
actualResult = name.endsWithIgnoreCase(suffix3);
assertFalse(actualResult);
actualResult = name.endsWithIgnoreCase(suffix4);
assertFalse(actualResult);
actualResult = name.endsWithIgnoreCase(suffix5);
assertTrue(actualResult);
}
public void testEndsWith() {
actualResult = name.endsWith(suffix0);
assertTrue(actualResult);
actualResult = name.endsWith(suffix1);
assertFalse(actualResult);
actualResult = name.endsWith(suffix2);
assertTrue(actualResult);
actualResult = name.endsWith(suffix3);
assertFalse(actualResult);
actualResult = name.endsWith(suffix4);
assertFalse(actualResult);
actualResult = name.endsWith(suffix5);
assertTrue(actualResult);
}
public static Test suite() {
return new TestSuite(StringTest.class);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
}
|