String的indexOf()用於獲取字符串中某個子字符串的位置


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM