Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6307387
Votes 0
Synopsis Add String.endsWithIgnoreCase(String suffix)
Category java:classes_lang
Reported Against
Release Fixed
State 11-Closed, duplicate of 4838773, request for enhancement
Priority: 4-Low
Related Bugs 4838773
Submit Date 06-AUG-2005
Description
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  customer  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>  customer 
+     *          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>  customer  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());
    }
}
Posted Date : 2005-08-06 00:20:58.0
Work Around
N/A
Evaluation
This is a DUP of 4838773.  One of [6307387, 4838773] should be closed.

My mistake - I didn't search carefully enough before opening 6307387.
Posted Date : 2006-01-13 23:39:46.0

Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=12039&forumID=1463
Posted Date : 2006-03-16 00:06:44.0

My mistake.  This should be closed as a duplicate of 4838773.
Posted Date : 2006-07-21 06:41:05.0
Comments
  
  Include a link with my name & email   

Submitted On 05-SEP-2005
mthornton
Rather than testing strings, what is really needed here is a good API for testing/obtaining the type of a file. This would also work in cases where the extension used varies in more than just case (.e.g. .HTM or .HTML) or on systems which don't use extensions at all.


Submitted On 05-SEP-2005
mthornton
See bug 4039759



PLEASE NOTE: JDK6 is formerly known as Project Mustang