• Post Reply Bookmark Topic Watch Topic
  • New Topic

indexOf ()  RSS feed

 
Ankit Gandhi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can anyone throw some focus on limitation of java function indexOf() ??
Recently we faced one issue regarding this while production release.

We were searching �abc.com� in the input string �abc.com�, which are exactly same. But it only searched till �abc� and skipped. So because of that the control was going to �else� block (means Strings are not equal)�and we were getting different result which was not required.

Hence can anyone confirm me that indexOf() does not check for �.� and it skips the string after �.� ??

TIA
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your code. I tried the following and it printed out 0 as expected.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

For more information, please read this.

This post has been moved to a more appropriate forum.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[pedantic mode]It's not a function, but a method. And please avoid saying things like TIA unless they have been explained in advance; they might confuse people who didn't grow up with the English language. TIA means something different to me from what I think it means to you; I assume you meant "Thanks in advance."[/pedantic mode]

Assuming you mean the String#indexOf(java.lang.String) method, on close examination it turns out the parameter is called "str". That suggests it behaves as an ordinary String and (as Joanne has shown you) it should match throughout. You get that sort of behaviour with String parameters called x, s, text, str etc.

If you have more problems, try putting the Strings through the equals and compareTo methods, to check that they really are identical. If that throws up a discrepancy, try printing all the individual chars using printf and the %d or %x tags, in case somebody has slipped in other encodings (it is possible to write abc.com in Russian and pronounce it ouce.somm)! Such other encodings are a potential marker of malware.

Beware whenever you find a method taking a String parameter and calling it "reg" "regex" or similar (example here). Those usually behave as regular expressions, in which case the . would be the metacharacter for anything. I presume that is what you were thinking about earlier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!