Home » Java programming language

Java StringBuilder indexOf() method with example

StringBuilder Class indexOf() method: Here, we are going to learn about the indexOf() method of StringBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 22, 2019

Syntax:

    public int indexOf (String s);
    public int indexOf (String s, int st_idx);

StringBuilder Class indexOf() method

  • indexOf() method is available in java.lang package.
  • indexOf (String s) method is used to search the index within this string of the first occurrence of the given string.
  • indexOf (String s, int st_idx) method is used to search the index within this string of the first occurrence of the given substring and searching will start from st_idx.
  • These methods may throw an exception at the time of returning then index of first occurrence string.
    NullPointerException – This exception may throw when the given string parameter is null.
  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, String s – represents the substring to search for.
  • In the second case, String s, int st_idx
    • String s – Similar as defined in the first case.
    • int st_idx – represents the index to start the searching from.

Return value:

The return type of this method is int, it returns the index within this object of the first occurrence of the given substring.

Example:

// Java program to demonstrate the example 
// of indexOf () method of StringBuilder class

public class IndexOf {
    public static void main(String[] args) {

        // Creating an StringBuilder object
        StringBuilder st_b = new StringBuilder("Java World ");

        // Display st_b 
        System.out.println("st_b =" + st_b);

        // By using indexOf("a") method is to return the first index of 
        // given string "a" in st_b object 
        // (first a at index 1 and second a at index 3)
        // it returns 1
        int index1 = st_b.indexOf("a");

        // Display st_b index
        System.out.println("st_b.indexOf(String) =" + index1);

        // By using indexOf("a",1) method is to return the first index of 
        // given string "a" in st_b object 
        // (first a at index 1 and second a at index 3)
        // it returns 1 and searching starts at index 1
        int index2 = st_b.indexOf("a", 1);

        // Display st_b index
        System.out.println("st_b.indexOf(String, int) =" + index2);
    }
}

Output

st_b =Java World 
st_b.indexOf(String) =1
st_b.indexOf(String, int) =1





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.