Java String LastIndexof示例
时间:2020-02-23 14:35:14 来源:igfitidea点击:
String的LastIndexof方法用于查找任何字符或者子字符串的最后索引。
LastIndexof方法可以将char或者string作为参数,也可以提供fromIndex。
方法:
public int lastIndexof(int ch)返回字符串中最后一次发生字符的索引
public int lastIndexof(int ch,int flowindex)返回字符串中最后一次发生字符的索引,从特定索引"fromIndex"public int lastIndexof(string)返回字符串公共INT lastIndexof中的子字符串的最后一次发生索引(Int Ch, int flofind)返回字符串内最后一次发生的子字符串的索引,从特定索引"fromIndex"后退
All lastIndexOf methods return -1 if char or substring is not present in the String
字符串索引示例:
package org.igi.theitroad; public class StringLastIndexOfExample { /* * @ Author : igi Mandliya */ public static void main(String args[]) { String str="Hello world from theitroad"; //Using lastIndexOf method with character System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with character"); System.out.println("---------------------------------------------------"); System.out.println("Last index of character l in "Hello world from theitroad" is : "+str.lastIndexOf('l')); System.out.println(); String str1="from"; System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with fromIndex with character"); System.out.println("---------------------------------------------------"); System.out.println("Last index of character o from index 6 in "Hello world from theitroad" is : "+str.lastIndexOf('o',6)); System.out.println(); //Using lastIndexOf method with String System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with String"); System.out.println("---------------------------------------------------"); System.out.println("Last index of String "from" in "Hello world from theitroad" is : "+str.lastIndexOf(str1)); System.out.println(); String howDo="How do you do"; System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with fromIndex with String"); System.out.println("---------------------------------------------------"); System.out.println("Last index of String "do" from index 9 in "How do you do" is : "+howDo.lastIndexOf("do",9)); System.out.println(); System.out.println("---------------------------------------------------"); System.out.println("If String or char is not available"); System.out.println("---------------------------------------------------"); System.out.println("Last index of "from" in "How do you do" is : "+howDo.lastIndexOf("from")); } }
运行上面的程序时,我们将得到以下输出:
------------------- -------------------------------------------------- Using lastIndexOf method with character -------------------------------------------------- Last index of character l in "Hello world from theitroad" is : 23 -------------------------------------------------- Using lastIndexOf method with fromIndex with character -------------------------------------------------- Last index of character o from index 6 in "Hello world from theitroad" is : 4 -------------------------------------------------- Using lastIndexOf method with String -------------------------------------------------- Last index of String "from" in "Hello world from theitroad" is : 12 -------------------------------------------------- Using lastIndexOf method with fromIndex with String -------------------------------------------------- Last index of String "do" from index 9 in "How do you do" is : 4 -------------------------------------------------- If String or char is not available -------------------------------------------------- Last index of "from" in "How do you do" is : -1