Java: StringBuffer indexOf() method

In this article, we will discuss StringBuffer’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
  • 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

Method signature:

public int indexOf(String str);

StringBufferIndexOfMethod.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferIndexOfMethod {

	public static void main(String[] args) {

		// StringBuffer
		StringBuffer sb = new StringBuffer("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 sub-string starting from specified index

  • Sample Java program to get 1st occurrence of specified sub-string starting from specified index-position as 2nd argument

Method signature:

public int indexOf(String str, int fromIndex);

StringBufferIndexOfMethod2.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferIndexOfMethod2 {

	public static void main(String[] args) {

		// StringBuffer
		StringBuffer sb = new StringBuffer("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: StringBuffer insert() method
Java: StringBuffer ensureCapacity() method