Java: StringBuilder indexOf() method

In this article, we will discuss StringBuilder’s indexOf() method which returns first occurrence of specified sub-string

 

indexOf() method:

  • This method is used to get first occurrence of specified sub-string i.e.; index-position of 1st occurrence of specified sub-string
  • Note: There are 2 variants or overloaded indexOf() methods

Method Signature:

public int indexOf(String str);

public int indexOf(String str, int fromIndex);

Parameters:

  • str                –> sub-string to be searched, to get its 1st occurrence
  • fromIndex  –> starting position from where search should begin

Returns:

indexOf() method

Returns

indexOf(String str); Returns 1st occurrence of specified sub-string

i.e.; index-position for 1st occurrence

indexOf(String str, int fromIndex); Returns 1st occurrence of specified sub-string starting from specified index

i.e.; index-position for 1st occurrence

Throws:

  • NullPointerException, if specified sub-string is null

 

Examples on indexOf() method:

1. To find 1st occurrence of specified sub-string

  • Sample Java program to get 1st occurrence of specified sub-string

StringBuilderIndexOfMethod.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderIndexOfMethod {

	public static void main(String[] args) {

		// StringBuilder
		StringBuilder sb = new StringBuilder("BenchResource.Net");

		// 1. To get index of substring 'Resource'
		int indexOfSubstring1 = sb.indexOf("Resource");

		// print to console
		System.out.println("1. Index of substring"
				+ " 'Resource' is : " + indexOfSubstring1);


		// 2. To get index of substring 'Net'
		int indexOfSubstring2 = sb.indexOf("Net");

		// print to console
		System.out.println("2. Index of substring"
				+ " 'Net' is      : " + indexOfSubstring2);
	}
}

Output:

1. Index of substring 'Resource' is : 5
2. Index of substring 'Net' is      : 14

 

2. To find 1st occurrence of specified substring starting from specified index

  • Sample Java program to get 1st occurrence of specified sub-string from specified index-position

StringBuilderIndexOfMethod2.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderIndexOfMethod2 {

	public static void main(String[] args) {

		// StringBuilder
		StringBuilder sb = new StringBuilder("BenchResource.Net");

		// 1. To get index of substring 'sour', 
		// starting from 6th index
		int indexOfSubstringfrom1 = sb.indexOf("sour", 6);

		// print to console
		System.out.println("1. Index of substring 'sour', "
				+ "starting from 6th position is : " 
				+ indexOfSubstringfrom1);


		// 2. To get index of substring 'Net', 
		// starting from 10th index
		int indexOfSubstringfrom2 = sb.indexOf("Net", 10);

		// print to console
		System.out.println("2. Index of substring 'Net', "
				+ "starting from 10th position is : " 
				+ indexOfSubstringfrom2);
	}
}

Output:

1. Index of substring 'sour', starting from 6th position is : 7
2. Index of substring 'Net', starting from 10th position is : 14

 

References:

 

Happy Coding !!
Happy Learning !!

Java: StringBuilder insert() method
Java: StringBuilder ensureCapacity() method