1

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!

9
  • Show some more of your code please. I just ran "20160719_P_BID_20160718_130000".contains("BID") and the result is "true" Commented Jul 18, 2016 at 9:52
  • Have you tried copying the exact String and pasting into the code? Commented Jul 18, 2016 at 9:52
  • .contains method should works. Would you post the exact code you tested with? Commented Jul 18, 2016 at 9:54
  • debug and check the value coming in variable fileName Commented Jul 18, 2016 at 9:57
  • You must print tradeTypeStr to see what is the value that you are comparing. Commented Jul 18, 2016 at 9:58

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! you've been most helpful!
0

Test this

String fileName = file.getName();

if(fileName.contains(tradeTypeStr))

1 Comment

Maybe you are changing your keyboard language when typing "BID"! Could you use copy and paste from debugging variable of fileName? It should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.