Java - String.lastIndexOf(str)逻辑我无法理解

Car*_*arl 1 java string indexing lastindexof

我使用两个不同的字符串来测试"\ t"的最后一个索引,但它们都返回4.我认为它应该是5和4.我检查了oracle文档,我无法理解为什么.有人可以告诉我为什么吗?谢谢!

System.out.println("abc\t\tsubdir".lastIndexOf("\t"));
System.out.println("abct\tsubdir".lastIndexOf("\t"));
Run Code Online (Sandbox Code Playgroud)

YCF*_*F_L 8

让我们让索引的数量更好地理解它:

字符串1

a b c \t \t s u b d i r
0 1 2  3  4 5 6 7 8 9 10
          ^-----------------------------------last index of \t (for that you get 4)
Run Code Online (Sandbox Code Playgroud)

字符串2

a b c t \t s u b d i r
0 1 2 3  4 5 6 7 8 9 10
         ^-----------------------------------last index of \t (for that you get 4)
Run Code Online (Sandbox Code Playgroud)

有些特殊字符应该被转义\(tab \t,breadline \n,quote \"...)在Java中,所以它算作一个字符,而不是2