BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuilder indexOf()

By Chaitanya Singh | Filed Under: java

In this guide, we will discuss the Java StringBuilder indexOf() method with the help of examples.

The syntax of indexOf() method is:

sb.indexOf("hello"); //searches string "hello" in the sb 
sb.indexOf("hello", 4) //starts searching string "hello" from index 4 

Here, sb is an object of StringBuilder class.

indexOf() Description

There are two variations of indexOf() method in Java StringBuilder class.

public int indexOf(String str): It returns the index of first occurrence of the string str in the StringBuilder instance.

public int indexOf(String str, int fromIndex): It starts the search from the specified index fromIndex and returns the index of string str.

indexOf() Parameters

The indexOf() method of Java StringBuilder class takes a single parameter:

  • str: The String str represents the character sequence that needs to be searched in the StringBuilder instance sb.
  • fromIndex: Represents an index from where the search starts.

indexOf() Return Value

  • It returns an integer value that represents the index of the first occurrence of specified string. If the specified string is not found then this method returns -1.

Example 1: Specified String without fromIndex

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Cool Book");
    System.out.println("Given String: " + sb);

    // first occurrence of string "oo"
    System.out.println("Index of string 'oo': "+sb.indexOf("oo"));
  }
}  

Output:

Java StringBuilder indexOf() example output1

Example 2: Searching string from a specified index

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Cool Book");
    System.out.println("Given String: " + sb);

    // Search string "oo" starting from index 3
    System.out.println("Occurrence of 'oo' after index 3 is: "+
            sb.indexOf("oo", 3));
  }
}  

Output:

Java StringBuilder indexOf() example output2

Example 3: When specified string is not found

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Cool Book");
    System.out.println("Given String: " + sb);

    // Index of string "Beginners" in the given string
    System.out.println("Index of 'Beginners' is: "+
            sb.indexOf("Beginners"));
  }
}  

Output:

Java StringBuilder indexOf() example output3

Example 4: String doesn’t exist after specified index

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Cool Book");
    System.out.println("Given String: " + sb);

    // Index of string "oo" after index 10
    System.out.println("Index of 'oo' after 10: "+sb.indexOf("oo",10));
  }
}  

Output:

indexOf() example output4

Recommended Posts

  • Java StringBuilder ensureCapacity()
  • Java StringBuilder substring()
  • Java StringBuilder charAt()
  • Java StringBuilder capacity()

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap