Java StringBuilder indexOf()方法及示例

来源:这里教程网 时间:2026-02-17 21:14:13 作者:

Java StringBuilder indexOf()方法及示例

在StringBuilder类中,有两种类型的indexOf()方法,取决于传递给它的参数。

indexOf(String str)

StringBuilder类的 indexOf(String str) 方法是一个内置的方法,用于返回作为参数的子串第一次出现时在字符串中的索引。如果子串str不存在,那么将返回-1。

语法

public int indexOf(String str)

参数: 本方法只接受一个参数 str ,它是字符串类型的值,指的是需要索引的字符串。

返回值: 该方法返回所传递的子串的第一次出现的 索引 ,如果没有这样的子串,则返回-1。

下面的程序说明了StringBuilder.indexOf()方法。

例1: 当传递的子串存在于序列中时。

// Java program to demonstrate// the indexOf() 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.indexOf("For");          // print results        System.out.println("index of string 'For' = "                           + index);    }}

输出:

String contains = GeeksForGeeksindex of string 'For' = 5

例子2: 当传递的子串在序列中不存在时。

// Java program to demonstrate// the indexOf() 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.indexOf("article");          // print results        System.out.println("Index of string"                           + " 'article' = "                           + index);    }}

输出:

String contains = Geeks for Geeks contributeIndex of string 'article' = -1

indexOf(String str, int fromIndex)

StringBuilder类的 indexOf(String str, int fromIndex) 方法是一个内置的方法,用于返回从指定的索引’fromIndex’开始的、作为参数的子串在字符串中第一次出现的索引。如果子串str不存在,则返回-1。 fromIndex 是整数类型的值,指的是开始搜索的索引。这个方法返回的索引是从序列的开始计算的,唯一的区别是搜索开始的索引是在这个方法中给出的。如果字符串在搜索开始的索引之前存在,但不在之后,那么将返回-1。

语法

public int indexOf(String str, int fromIndex)

参数: 该方法接受两个参数。

str: 字符串类型的值,是指需要索引的字符串。fromIndex : 整数类型的值,指的是要开始搜索的索引。

返回值: 该方法返回从指定索引开始的第一次出现的子串的 索引 ,如果没有这样的子串,则返回-1。

下面的程序说明了StringBuilder.indexOf()方法。

例1: 当传递的子串存在于序列中时。

// Java program to demonstrate// the indexOf() 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 Form index 3        int index = str.indexOf("For", 3);          // print results        System.out.println("index of string"                           + " \"For\" = "                           + index);    }}

输出:

String contains = GeeksForGeeksindex of string "For" = 5

例2: 当通过的子串在序列中存在,但是搜索的索引大于子串的索引。

// Java program to demonstrate// the indexOf() 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.indexOf("Geeks", 15);          // print results        System.out.println("index of string 'Geeks ' = "                           + index);    }}

输出:

String contains = Geeks for Geeks contributeindex of string 'Geeks ' = -1

参考资料

https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#indexOf(java.lang.String, int)https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#indexOf(java.lang.String)

相关推荐