[JAVA]String-字串搜尋的方法: lastIndexOf 、 offsetByCodePoints 


intindexOf(int ch)
返回指定字元在此字元串中第一次出現處的索引。
intindexOf(int ch, int fromIndex)
返回在此字元串中第一次出現指定字元處的索引,從指定的索引開始搜尋。
intindexOf(String str)
返回指定子字元串在此字元串中第一次出現處的索引。
intindexOf(String str, int fromIndex)
返回指定子字元串在此字元串中第一次出現處的索引,從指定的索引開始。


使用方式:

                String string7 = "javajava";
System.out.println(string7.indexOf('a'));//1
System.out.println(string7.indexOf('a',2));//3
System.out.println(string7.indexOf("java"));//0
System.out.println(string7.indexOf("java",2));//4


intlastIndexOf(int ch)
返回指定字元在此字元串中最後一次出現處的索引。
intlastIndexOf(int ch, int fromIndex)
返回指定字元在此字元串中最後一次出現處的索引,從指定的索引處開始進行反向搜尋。
intlastIndexOf(String str)
返回指定子字元串在此字元串中最右邊出現處的索引。
intlastIndexOf(String str, int fromIndex)
返回指定子字元串在此字元串中最後一次出現處的索引,從指定的索引開始反向搜尋。


使用方式:

                System.out.println(string7.lastIndexOf('a'));//7
System.out.println(string7.lastIndexOf('a',2));//1
System.out.println(string7.lastIndexOf("java"));//4
System.out.println(string7.lastIndexOf("java",2));//0



intoffsetByCodePoints(int index, int codePointOffset)
返回此 String 中從給定的 index 處偏移 codePointOffset 個程式碼點的索引。


使用方式:

                String string11 = "abdetuiop";
System.out.println(string11.offsetByCodePoints(1,2));//3



參考:
String API

 


















其它文章

arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()