Waypoint
Waypoint

Reputation: 17763

Java - startsWith error

I am using method startsWith to find out, whether my string starts with desired string. Example:

       for(int i=0;i<tokens.length;i++){
           if(tokens[i].startsWith(ColumnName)){
                tokens[i]="";

           }

In tokens[i] there is a string "info REAL", in ColumnName, there is a string "info". In this comparsion, every time i get false. It is unbelivable, but even when i print it, it is like - tokens[i]:info REAL, startsWith:info, result:false...

I don't see any mistake here, you do? There is no TYPO in my program, I am 100% sure theese values are here correctly.

Thanks

Upvotes: 0

Views: 1069

Answers (2)

Valentin Rocher
Valentin Rocher

Reputation: 11669

Maybe it's caused by whitespaces. Have you tried trimming your strings before doing that ?

Something like tokens[i].trim().startsWith(ColumnName.trim())

Upvotes: 1

styx
styx

Reputation: 421

check for spaces in the strings, its easy to overlook them in the console output.

e.g. "info REAL" vs "info " or "info REAL" vs " info"

Upvotes: 2

Related Questions