• Post Reply Bookmark Topic Watch Topic
  • New Topic

Evaluating String indexof  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1782
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code why if value is "" does indexof get evaluated to 0:


spoc exclusion list = ('S070','S100','S110','S120','S130','S150','S200')

 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because the first place in the string "S070" which matches a zero-length string is at the beginning, i.e. position zero. If that's a little too obscure for you, then have a look at the API documentation for the startsWith() method of String, which says

"Returns true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset; false otherwise."

And if you look at it that way, then "" is indeed the first zero characters of "S070".
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it indexOf that gets evaluated to 0, or the result of that last line? Because the last line says that if indexOf returns anything larger than -1, 100 should be used and otherwise 0. In other words, if indexOf returns -1 then 0 must be used.
 
Steve Dyke
Ranch Hand
Posts: 1782
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Is it indexOf that gets evaluated to 0, or the result of that last line? Because the last line says that if indexOf returns anything larger than -1, 100 should be used and otherwise 0. In other words, if indexOf returns -1 then 0 must be used.


No it always returns 100 even when item.getSpocno() == "".

The only I could get it to work was to replace the item.spoc = "" with item.spoc = "N/A" in the item class property. This is the item class:

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!