Google+

157. Using 'lastIndexOf( ) method with StringBuffer







lastIndexOf( ) method when used with StringBuffer searchers for the last occurrence of a substring in two formats and returns index value.

Example: (Search for the last occurrence of a substring from the beginning of the string till its end)

StringBuffer object1 = new StringBuffer("This is a test statement of a test person in a test environment");
object1.lastIndexOf("test"); -> It will search for the "test" substring from the beginning of the string "This is a test statement of a test person in a test environment" till the end of the string and give the index value of the last occurrence of the substring i.e. 47 in this example


Example: (Search for the last occurrence of a substring from beginning till the provided index of the string)

StringBuffer object1 = new StringBuffer("This is a test statement of a test person in a test environment");
object1.lastIndexOf("test",11); -> It will search for the "test" substring from beginning till the provided index 11 of the string "This is a test statement of a test person in a test environment"and give the index value of the last occurrence of the substring i.e. 10

Note: - IndexOf( ) and lastIndexOf( ) methods return '-1' as index value/result if the provided substring is not found in the string.


Lets implement this on Eclipse IDE:

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



2. Save and Run the 'lastIndexOfDemo' class
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 shortcuts for System.out.println( ) statement in Eclipse IDE will be explained in the next post. 





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.



155. Using 'substring( )' method with StringBuffer








substring( ) method when used with StringBuffer extracts a portion from the provided string using the following two formats:

Example: (Format 1)

StringBuffer object1 = new StringBuffer("SunShine");
object1.substring(3);  -> This will extract the text in "SunShine" from index 3 till the end i.e. It will extract "Shine" in this example.

Example: (Format 2)


StringBuffer object2 = new StringBuffer("SuperSunShine");
object1.substring(5,8);  -> This will extract the text in "SuperSunShine" from index 3 till index (8-1) i.e. It will extract "Sun" in this example.

Lets implement this on Eclipse IDE:

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
























2. Save and Run the 'substringDemo' 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 'indexOf( )' method with StringBuffer will be explained in the next post.




154. Using 'replace( )' method with StringBuffer







replace( ) method when used with StringBuffer replaces the set of characters with another set of characters.

Example:

StringBuffer object1 = new StringBuffer("I hate you");
object1.replace(2,6,"love");  -> Replaces the "hate" in "I love you" with "love". The object1 will contain "I love you" string after replacing.


Lets implement this on Eclipse IDE:


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

























2. Save and Run the 'replaceDemo' 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 'substring( )' method with StringBuffer will be explained in the next post.




153. Using 'deleteCharAt( )' method with StringBuffer







deleteCharAt( ) method when used with StringBuffer deletes a character from the StringBuffer's object at the provided index.

Example:

StringBuffer object1 = new StringBuffer("ABCZDEF");
object1.deleteChatAt(3); -> Deletes the character available at index 3 i.e. Z in "ABCZDEF"

Lets implement this on Eclipse IDE:

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



2. Save and Run the 'deleteChatAt' 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 'replace( )' method with StringBuffer will be explained in the next post.



152. Using 'delete( )' method with StringBuffer






delete( )  method when used with StringBuffer deletes a set of characters from the StringBuffer's object.

Example:

StringBuffer object1 = new StringBuffer("ABCD1234EFGH");
object1.delete(4,8); -> Deletes characters from index 4 to index (8-1). i.e. deletes "1234" from "ABCD1234EFGH".

Lets implement this on Eclipse IDE:

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
























2. Save and Run the 'deleteDemo' 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 'deleteCharAt( )' method with StringBuffer will be explained in the next post.



151. Using 'reverse( )' method with StringBuffer







reverse( )  method when used with StringBuffer reverses the characters in StringBuffer's object.

Example:

StringBuffer object1 = new StringBuffer("a12345z");
object1.reverse( ); -> This will reverse the string "a12345z" to "z54321a"

Lets implement this on Eclipse IDE:

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



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



Download this project:

Click here to download this 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 'delete( )' method with StringBuffer will be explained in the next post.



150. Using 'insert( )' method with StringBuffer







insert( ) method when used with StringBuffer inserts one string into another string.

Example:

StringBuffer object1 = new StringBuffer("ABCDIJKL");
StringBuffer object2 = new StringBuffer("ABD");
StringBuffer object3 = new StringBuffer("ABEF");
String str = "EFGH";
char ch = 'C'
StringBuffer object4 = new StringBuffer("CD");

object1.insert(4, str); -> This will insert "EFGH" at index 4 in "ABCDIJKL". After inserting, the object1 will contain "ABCDEFGHIJKL" string.
object2.insert(2, ch); -> This will insert 'C' charater at index 2 in "ABD". After inserting, the object2 will contain "ABCD" string.
object3.insert(2, object4); -> This will insert "CD" at index 2 in "ABEF". After inserting, the object3 will contain "ABCDEF" string.

Lets implement this on Eclipse IDE:

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



2. Save and Run the 'insertDemo' 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 'reverse( )' method with StringBuffer will be explained in the next post.




149. Using append( ) method with StringBuffer







append( ) method when used with StringBuffer concatenates the provided data to the end of the invoking StringBuffer object.

Example:

StringBuffer object1 = new StringBuffer("Start");
int num = 123;
String str = "abc";
StringBuffer object2 = new StringBuffer("Stop");

object1.append(num).append(456).append(str).append("xyz").append(object2);  -> This statement will append 123,456,abc,xyz and Stop to the "Start" text -> i.e. This will result Start123456abcxyzStop

Lets implement this on Eclipse IDE:

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



2. Save and Run the 'appendDemo' 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 'insert( )' method with StringBuffer will be explained in the next post.



148. Using 'getChars( )' method with StringBuffer







getChars( ) when used with StringBuffer will extract the substring from the specified string and copies the extracted substring into a char array.

Example:

StringBuffer object1 = new StringBuffer("SuperSunShine");
char targetCharArray[ ] = new char[3];

object1.getChars(5,8, targetCharArray, 0); -> This will extract the substring from the specified string "SuperSunShine" using the provided Start Index 5 and End Index (8-1) i.e. "Sun" in the specified string "SuperSunShine" and copies the extracted substring "Sun" to char array "tagetCharArray" from targetCharArray[0] i.e. 0 index.

Lets implement this on Eclipse IDE:

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



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



Download this project:

Click here to download this 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 append( ) method with StringBuffer will be explained in the next post.



147. Using 'setCharAt( )' method with StringBuffer







setCharAt( )  method when used with StringBuffer replaces the character with another at the specified index.

Example:

StringBuffer object1 = new StringBuffer("man");

object1.setCharAt(1,'e');  -> This will replace the character available at the provided index '1' in the specified string "man" i.e. 'a' with another character 'e'. The resultant string after replacement is "men"

Lets implement this on Eclipse IDE:

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



2. Save and Run the 'setCharAtDemo' 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 'getChars( )' method with StringBuffer will be explained  in the next post.



146. Using 'charAt( )' method with StringBuffer







charAt( ) method when used with StringBuffer extracts the character from the specified string using the provided index.

Example:

StringBuffer object1 = new StringBuffer("abcdef");

object1.charAt(3);   -> Will extract the character at index 3 in the specified string "abcdef" i.e. 'd'

Lets implement this on Eclipse IDE:

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



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



Download this project:

Click here to download this 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 'setCharAt( )' method with StringBuffer will be explained in the next post.



145. Using 'length( )' method with StringBuffer







length( ) method when used with StringBuffer gives the current length of the StringBuffer's object.

Example:

StringBuffer object = new StringBuffer("abcdef");

object.length( );  -> Gives the length of the object it is holding now (i.e. the length of "abcdef" text in this example -> i.e. 6 )

Lets implement this on Eclipse IDE:

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



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



Download this project:

Click here to download this 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 'charAt( )' method with StringBuffer will be explained in the next post.