Duplicate :
|
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.