Java: StringBuffer indexOf() method

In this article, we will discuss StringBuffer’s indexOf() method which returns first occurrence of specified substring

 

indexOf() method:

  • This method is used to get first occurrence of specified substring 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                    –> substring 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 substring (i.e.; index-position for 1st occurrence)
indexOf(String str, int fromIndex); Returns 1st occurrence of specified substring starting from specified index (i.e.; index-position for 1st occurrence)

 

Throws:

  • NullPointerException, if specified substring is null

 

Examples on indexOf() method:

Sample Java program to get 1st occurrence of specified substring using StringBuffer’s indexOf() method

1. To find 1st occurrence of specified substring

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 substring starting from specified index

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:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: StringBuffer insert() method
Java: StringBuffer ensureCapacity() method