Java StringBuilder lastIndexOf()方法及示例
在StringBuilder类中,有两种类型的lastIndexOf()方法,取决于传递给它的参数。
lastIndexOf(String str)
StringBuilder类的 lastIndexOf(String str) 方法是一个内置的方法,用于返回作为参数的子串最后出现在String中的索引。最后出现的空字符串””被认为发生在索引值this.length()处。如果子串str不存在,那么将返回-1。
语法 :
public int lastIndexOf(String str)
参数: 该方法只接受一个参数 str ,它是一个字符串类型的值,指的是我们想得到的最后出现的字符串的索引。
返回值: 该方法返回所传递的子串的最后出现的 索引 ,如果没有这样的子串,则返回-1。
下面的程序说明了java.lang.StringBuilder.lastIndexOf()方法。
例1: 当传递的子串在序列中存在时。
// Java program to demonstrate// the lastIndexOf() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("GeeksForGeeks"); // print string System.out.println("String contains = " + str); // get index of string For int index = str.lastIndexOf("Geeks"); // print results System.out.println("Index of last occurrence" + " string \"Geeks\"= " + index); }}
输出:
String contains = GeeksForGeeksIndex of last occurrence string "Geeks"= 8
例子2: 当传递的子串在序列中不存在时。
// Java program to demonstrate// the lastIndexOf() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder( "Geeks for Geeks contribute"); // print string System.out.println("String contains = " + str); // get index of string article int index = str.lastIndexOf("article"); // print results System.out.println("Index of string" + " 'article' = " + index); }}输出:
String contains = Geeks for Geeks contributeIndex of string 'article' = -1
lastIndexOf(String str, int fromIndex)
StringBuilder类的 lastIndexOf(String str, int fromIndex) 方法是一个内置的方法,用于返回原始字符串中子串的索引。但是子串的搜索是从0索引到 fromIndex索引 开始的 。 在这个新的范围内(0-fromIndex),现在找到了子串的最后一次出现,并由这个函数返回起始索引。如果在这个范围内没有找到子串,就会返回-1。请注意,搜索子串最后一次出现的范围的起点始终是0。用户只能设置结束点。
语法 :
public int lastIndexOf(String str, int fromIndex)
参数: 该方法接受两个参数。
str 是字符串类型的值,指的是我们试图得到的子串的索引。fromIndex ,整数类型的值,指的是要从哪一个索引开始向后搜索。返回: 该方法返回从指定索引开始的最后出现的子串的 索引 ,如果没有这样的子串,则返回-1。
下面的程序说明了StringBuilder.lastIndexOf()方法。
例1: 当传递的子串在序列中存在时。
// Java program to demonstrate// the lastIndexOf() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("WeGeeksLoveGeeksForGeeks"); // print string System.out.println("String contains = " + str); // get index of last occurrence of substring till 15th position of original String int index = str.lastIndexOf("Geeks", 15); // print results System.out.println("index of last occurrence" + " string \"Geeks\" = " + index); }}输出:
String contains = WeGeeksLoveGeeksForGeeksindex of last occurrence string "Geeks" = 11
例2: 当fromIndex小于子串的最后出现的索引时
// Java program to demonstrate// the lastIndexOf() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("Geeks for Geeks contribute"); // print string System.out.println("String contains = " + str); // get index of string Geeks from index 15 int index = str.lastIndexOf("contribute", 10); // print results System.out.println("index of string" + " 'contribute ' = " + index); }}输出:
String contains = Geeks for Geeks contributeindex of string 'contribute ' = -1
参考资料:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#lastIndexOf(java.lang.String, int)https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#lastIndexOf(java.lang.String)