iT 邦幫忙 | java indexof用法
indexOf()Method的作用是檢視特定字元(specifiedcharacter(s))的位置,而它所返回的值會是那個特定字元第一次出現的位置。語法–有4個methodspublicintindexOf(Stringstr)publicintindexOf(Stringstr,intfromIndex)publicintindexOf(intchar)publicintindexOf(intchar,intfromIndex)indexOf(Stringstr)解釋:當中,str是指要搜尋的特定字元將會返回那個特定字元第一次出現的位置例子1:publicclassIndexOfExercise{publicstaticvoidmain(Stringargs[]){//定義StringStringtestcase1=newString("HiMartinYeung");Stringsubst=newString("M...
indexOf() Method的作用是檢視特定字元(specified character(s))的位置,而它所返回的值會是那個特定字元第一次出現的位置。
語法 – 有4個methodspublic int indexOf(String str)public int indexOf(String str, int fromIndex)public int indexOf(int char)public int indexOf(int char, int fromIndex)
indexOf(String str) 解釋:當中,str是指要搜尋的特定字元將會返回那個特定字元第一次出現的位置
例子1:
public class IndexOfExercise { public static void main(String args[]) { // 定義 String String testcase1 = new String("Hi Martin Yeung"); String subst = new String("Martin"); System.out.print("找出第1個Martin的位置 : "); // 第1個i是出現在位置1, 所以要由下一個位置開始搜尋,那就是由位置2開始 System.out.println(testcase1.indexOf(subst)); //最後會出現 = 找出第1個Martin的位置 : 3 } }indexOf(int char) 解釋:當中,char是指要搜尋的特定字元將會返回那個特定字元第一次出現的位置
例子2:
public class IndexOfExercise { public static void main(String args[]) { // 定義 String ...