Google+

156. Using 'indexOf( )' method with StringBuffer







indexOf( ) when used with StringBuffer searches index value of first occurrence of substring in two formats:

Example: (Searches first occurrence of a substring from beginning of a string)

StringBuffer object1 = new StringBuffer("This is a test statement of a test person in a test environment");
object1.indexOf("test"); -> This statements give the result as index value of the first occurrence of substring "test" form the beginning of a string "This is a test statement of a test person in a test environment" i.e. 10

Example: (Searches first occurrence of a substring from the specified index of a string)

StringBuffer object1 = new StringBuffer("This is a test statement of a test person in a test environment");
object1.indexOf("test", 11); -> This statements give the result as index value of the first occurrence of substring "test"  from index 11 of  the string "This is a test statement of a test person in a test environment" i.e.  30


Lets implement this on Eclipse IDE:

1. Create 'indexOfDemo' class under any project as shown below:

























2. Save and Run the 'indexOfDemo' class file
3. Observe that the output is displayed in the console as shown below:




Download this project:


Click here to download the project containing the class file used in this post (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.

Using 'lastIndexOf( )' method with StringBuffer will be explained in the next post.



No comments: