• August 28, 2014
  • Core Java
  • Comments Off on replace(int startIndex, int endIndex, String str) StringBuilder method in java

replace(int startIndex, int endIndex, String str) StringBuilder method in java

replace(int startIndex, int endIndex, String str): replace the substring of the string builder from startIndex to endIndex-1 with specified string.

Syntax: public StringBuilder replace(int startIndex, int endIndex, String str).

Note: startIndex should be between 0 to length of string or less than endIndex, if it is not StringIndexOutOfBoundsException will be thrown.

Example:

StringBuilderReplaceExample.java

/**
 * This program is used to show the use of replace() method.
 * @author javawithease
 */
class TestStringBuilder{
	StringBuilder sb = new StringBuilder("www.abc.com");
 
	/**
	 * This method is used to show the use of replace() method.
	 * @author javawithease
	 */
	public void replaceTest(){
		//replace the substring of the string buffer from 
		//startIndex to endIndex-1 with specified string.
		System.out.println(sb.replace(4,7,"javawithease"));
	}
}
 
public class StringBuilderReplaceExample {
	public static void main(String args[]){
		//creating TestStringBuilder object
		TestStringBuilder obj = new TestStringBuilder();
 
		//method call
		obj.replaceTest();
	}
}

Output:

www.javawithease.com

Download this example.
 
Next Topic: delete(int startIndex, int endIndex) StringBuilder method in java.
Previous Topic: insert(int offset, String str) StringBuilder method in java.

Related Topics:

StringBuilder in java.
append(String str) StringBuilder method in java.
insert(int offset, String str) StringBuilder method in java.
replace(int startIndex, int endIndex, String str) StringBuilder method in java.
delete(int startIndex, int endIndex) StringBuilder method in java.
reverse() StringBuilder method in java.
capacity() StringBuilder method in java.
ensureCapacity(int minCapacity) StringBuilder method in java.
StringTokenizer in java.

 


Comments are closed.