Java 中 charAt() 和 indexOf() 的區別 ?
String 類的 charAt() 方法返回指定索引處的字元值。索引範圍為 0 至 length() - 1。序列的第一個字元值在索引 0 處,下一個在索引 1 處,依此類推,如同陣列索引。
String 類的 indexOf(int ch, int fromIndex) 方法返回該字串中第一個指定字元出現的索引,從指定索引處開始搜尋。
如果值等於 ch 的字元出現在此 String 物件表示的字元序列中,其索引不小於 fromIndex,則返回第一個此類出現的索引。
示例
public class CharAt_IndexOf_Example {
public static void main(String args[]){
String str = "This is tutorialspoint";
System.out.println("Index of letter 't' = "+ str.indexOf('t', 14));
System.out.println("Letter at the index 8 is ::"+str.charAt(8));
}
}輸出
Index of letter 't' = 21 Letter at the index 8 is ::t
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP