Ash Notts
Ash Notts

Reputation: 61

Java String Object .Equals not working as intended

I am pulling my hair out over what should be a simple problem

I am trying to compare two string objects using .equals but they do not match

if (folderJobNumber.toUpperCase().equals(fileJobNumber.toUpperCase())) {
                dostuff;
            }

Each string object is showing as "Y019/" in debug Please note that for both i had used substring to get their value as per below

String folderJobNumber = folderLocation.substring(folderLocation.length() - 5).trim();
String fileJobNumber = file.getName().substring(0,4) + "/".trim();

Please see screenshot below of each variable in debug

enter image description here

I have a feeling that the problem is this value which appears to be a char array of the data before using substring.

Thankyou in advanced for any light that you may shed on the problem

Upvotes: 0

Views: 365

Answers (2)

M. Plata
M. Plata

Reputation: 11

You can try to use equalsIgnoreCase instead of equals if you just want to compare the two strings.

if (folderJobNumber.equalsIgnoreCase(fileJobNumber)) {
                dostuff;
}

Upvotes: 1

mrpw
mrpw

Reputation: 36

String folderJobNumber = folderLocation.substring(folderLocation.length() - 5).trim(); String fileJobNumber = file.getName().substring(0,4) + "/".trim();

And looking at the debug screenshot, folderJobNumber is char[56], so I assume that is where the problem is...I am not sure how the folderLocation string was obtained, assuming it is in this fashion

char data[] = {'a', 'b', 'c'};
     String str = new String(data);

I would try

String folderJobNumber = folderLocation.trim().substring(folderLocation.length() - 5)

Upvotes: 0

Related Questions