Search code examples
javastringsubstringequalsindexof

String contains method return false even when string contains substring


I have a string:

20160719_P_BID_20160718_130000

I need to check if this string contains the substring "BID". I have tried various methods like:

  1. .contains("BID") / .contains("_BID_")
  2. .indexOf("BID")
  3. .substring(11,14).equals("BID)

but all methods have returned false even though the output of the strings does contain the string "BID" and is the string "BID"

Update:

String fileName = file.getFileName();
String tradeTypeStr = fileName.substring(11,14);        
if(tradeTypeStr.equalsIgnoreCase(tradeType))

Can someone shed some light as to why the methods are returning false?

Thanks for your help!


Solution

  • Since String.contains() and String.indexOf() definitely works, this sounds like you may have a problem with your encoding. Try

        String fileName = file.getName();
        for (char c : fileName.toCharArray())
        {
            System.out.println(c + " : " + Integer.toHexString(c));
        }
    

    If you don't find something like

    _ : 5f
    B : 42
    I : 49
    D : 44
    _ : 5f
    

    you've got the reason for your problem