Java – StringBuffer indexOf() method

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

1. StringBuffer’s 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

1.1 Method Signature:

public int indexOf(String str);

public int indexOf(String str, int fromIndex);

1.2 Parameters:

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

1.3 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)

1.4 Throws:

  • NullPointerException, if specified sub-string is null

2. Examples on indexOf() method:

2.1 To find 1st occurrence of specified sub-string

  • Below illustration depicts how 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.2 To find 1st occurrence of specified sub-string starting from specified index

  • Below illustration depicts how 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

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - StringBuffer insert() method
Java - StringBuffer ensureCapacity() method