CodeGym /Java 博客 /随机的 /Java String lastIndexOf() 方法
John Squirrels
第 41 级
San Francisco

Java String lastIndexOf() 方法

已在 随机的 群组中发布
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
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION