• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion about indexOf method

 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There is a snippet of code:




Options of the output are:-


and the output of the code is : -1

How it is working please explain it.

Thank you
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String's indexOf method returns -1 if the substring is not found. I don't see a (lowercase) 'f' in "Foolish".
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok but if there is

then what output should be:
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it and find out.
 
Sergey Baranov
Greenhorn
Posts: 18
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

indexOf

public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
this.startsWith(str, k)
is true.

Parameters:
str - any string.

Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wouter Oet ,


I run this program and output is 0.

But actually I am not understanding that how it works so please give me some explanation which will helpful for me to understand it easily.

Thanks in advance....
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the positions of "Foolish" are counted like this:

F -> 0
o -> 1
o -> 2
l -> 3
i -> 4
s -> 5
h -> 6

So, "Fool" matches starting at the 0th position.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The indexOf() method searches in the string that you call it on, where the string that you pass as an argument appears - exactly what the API documentation that Sergey quoted says.

The word "Fool" appears in the string "Foolish boy." at the beginning - index 0.

Can you explain in more detail what exactly you don't understand about that?
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Jasper,

In actual I was confused that how it matches to subString but Thank you of all you for giving me a good explanation I understand now this so thank you once again...............
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic