CodesJava

Easy learning with example program codes

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


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

Syntax: public synchronized  StringBuffer 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:

StringBufferReplaceExample.java

/**
 * This program is used to show the use of replace() method.
 * @author codesjava
 */
class TestStringBuffer{
	StringBuffer sb = new StringBuffer("www.abc.com");
 
	/**
	 * This method is used to show the use of replace() method.
	 * @author codesjava
	 */
	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,"codesjava"));
	}
}
 
public class StringBufferReplaceExample {
	public static void main(String args[]){
		//creating TestStringBuffer object
		TestStringBuffer obj = new TestStringBuffer();
 
		//method call
		obj.replaceTest();
	}
}

Output:

www.codesjava.com

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

Related Topics:

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

 

Please follow and like us:
Posted in Java   

Core Java Tutorial



Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki