JDK-6973550 : String::indexOf method return incorrect value when searching for CRLFCRLF
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 6u20
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux_redhat_4.0
  • CPU: x86
  • Submitted: 2010-07-30
  • Updated: 2010-08-23
  • Resolved: 2010-08-23
Related Reports
Duplicate :  
Description
Consider the following method : 
public void indexOfTest(String str1, String str2)
{
    if (!str1.equals(str2)) {
        System.out.println("Strings are not equal");
        return;
    }
    int index1 = str1.indexOf("\r\n\r\n");
    int index2 = str1.indexOf("\n\n");

    int index3 = str2.indexOf("\r\n\r\n");
    int index4 = str2.indexOf("\n\n");

    if (index1 != index3) {
        System.out.println("indexOf behavior of 2 equal strings is different. index1 = "
                            + index1 + " index3 = " + index3);
    }
    if (index2 != index4) {
        System.out.println("indexOf behavior of 2 equal strings is different. index2 = "
                            + index2 + " index4 = " + index4);
    }
}

When 2 strings are equal, their indexOf value should be the same. Isn't it?
The above method first checks if 2 strings are same, if they are same then it invokes indexOf method and looks for "\r\n\r\n", one of the string *sometimes* return nonzero value when it should return -1. Hence the test fails.