indexOf作用:用于检索一个字符串在另一个字符串中的位置。

indexOf的几个重载方法如下:

int indexOf(String str)  意思为在字符串中检索str第一次出现的位置,如果找不到返回-1。

       eg: String str = "I can because i think i can";

                int index = str.indexOf("can");

                System.out.println(index); // 2  从0开始,空格也算一位

int indexOf(String str,int fromIndex) 意思为  从字符串的fromIndex位置开始检索。

 eg: String str "I can because i think i can";           

          index = str.indexOf("can", 6);

          System.out.println(index); // 24

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
猜你喜欢
  • 2021-07-05
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
相关资源
相似解决方案