lastIndexOf ()方法返回指定字符或子字符串在字符串中最後一次出現的位置。想像一下,您有一些長文本,或者更確切地說是一行長文本。例如,它可以是一封信,您需要根據您已知的姓名找到最後一次給收件人打電話的地點。對於這種情況,Java String類的indexOf方法就非常適合。如果你需要一個字符在字符串中的第一次出現,你可以使用indexOf()方法,它與lastIndexOf()非常相似。lastIndexOf()有四種變體方法。由於方法重載,可以使用四個名稱相同但參數不同的方法。下面我們將通過示例查看此方法的所有四種變體。
lastIndexOf(int ch)
此方法返回字符序列中最後一次出現的字符的索引。方法的語法
int lastIndexOf(int ch)
參數: ch:一個字符。
代碼示例
public class LastIndexOf1 {
public static void main(String args[])
{
//letter to find index in String
char letter = 'd';
//String to find an index of a letter
String myString = "This is major Tom to ground control, do you copy";
//The index of last appearance of d will be printed
System.out.println("Last Index of d = " + myString.lastIndexOf(letter));
}
}
輸出是:
d 的最后索引 = 37
如果我們要查找的字符不在我們的字符串中,則該方法返回 -1:
public class LastIndexOf1 {
public static void main(String args[])
{
char letter = 'z';
String myString = "This is major Tom to ground control, do you copy";
System.out.println("Last Index of z = " + myString.lastIndexOf(letter));
}
}
輸出是:
z 的最后索引 = -1
lastIndexOf(int ch,int fromIndex)
lastIndexOf(int ch, int fromIndex):如果此字符在字符串中表示,則此方法返回最後一次出現 ch 字符的索引,從指定索引開始向後搜索。如果此字符未在子字符串中表示,則返回 -1。方法的語法
public int lastIndexOf(int ch, int fromIndex)
參數: ch:一個字符。 fromIndex:開始搜索的索引。
lastIndexOf(int ch, int fromIndex) 的代碼示例
public class LastIndexOf2 {
public static void main(String args[])
{
//letter to find index in String
char letter = 'o';
//String to find an index of a letter
String myString = "This is major Tom to ground control, do you copy";
//The index of last appearance of o before 20th symbol will be printed
System.out.println("Last Index of o = " + myString.lastIndexOf(letter, 20));
}
}
輸出是:
o 的最后索引 = 19
如果從索引傳遞到行首時未遇到該字符,則該方法將返回 -1:
public class LastIndexOf2 {
public static void main(String args[])
{
char letter = 'o';
String myString = "This is major Tom to ground control, do you copy";
System.out.println("Last Index of o = " + myString.lastIndexOf(letter, 10));
}
}
輸出是:
o 的最后索引 = -1
lastIndexOf(字符串 str)
lastIndexOf(String str):該方法的這種變體接受一個字符串作為參數,並返回該字符串中第一次出現的指定子字符串的索引。如果它不作為子字符串出現,則該方法返回 -1。方法的語法
public int lastIndexOf(String str)
參數: str:一個字符串。
lastIndexOf(String str) 的代碼示例
public class LastIndexOf3 {
public static void main(String args[])
{
String myString = "This is major Tom to ground control, do you copy";
System.out.println( myString.lastIndexOf("Tom"));
}
}
輸出是:
14
如果沒有這樣的子字符串,該方法返回 -1。讓我們嘗試找到子字符串“tom”開頭的索引。
public class LastIndexOf3 {
public static void main(String args[])
{
String myString = "This is major Tom to ground control, do you copy";
System.out.println( myString.lastIndexOf("tom"));
}
}
請記住,“T”和“t”是不同的符號,因此此字符串中沒有“tom”。這是輸出:
-1
lastIndexOf(字符串 str,int fromIndex)
lastIndexOf(String str, int fromIndex)。此方法的變體返回指定子字符串最後一次出現的此字符串中的索引,從指定索引開始向後搜索。方法的語法
public int lastIndexOf(String str, int beg)
參數 str:一個字符串。 fromIndex:開始搜索的索引。
lastIndexOf(String str, int fromIndex) 的代碼示例
讓我們嘗試在字符串“This is major Tom to ground control, do you copy”中找到子字符串“ro”最後一次出現的索引。第一次我們將遍歷整個字符串,第二次我們從索引為 25 的字符開始(我們記得,在上限約束下,索引的搜索從末尾到開頭)。
public class LastIndexOf4 {
public static void main(String[] args) {
String myString = "This is major Tom to ground control, do you copy";
System.out.println( myString.lastIndexOf("ro"));
System.out.println(myString.lastIndexOf("ro",25));
}
}
輸出是:
32 22
GO TO FULL VERSION