String.indexOf 给出的值比预期小一

String.indexOf gives a value one less than expected

我不理解 String.index Of 方法的行为。

String peter = "Peter Piper picked a peck of pickled pepper";
  System.out.println("The number of p in the sentence is, " + peter.indexOf('p')); 

为什么p的输出是8而不是9?句子中有9个P。

索引从零开始:

        ↓
Peter Piper picked a peck of pickled pepper
          111111111122222222223333333333444
0123456789012345678901234567890123456789012
        ↑

第一个 p 位于索引 8

来自 indexOf() 的 javadoc:

Returns the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.