Thursday, 19 February 2015

How To Replace Words In String

Another helpful method in StringBuffer  is replace() which is very useful when we want to replace some words in a string.It replace one set of character with another.Its signature shows below

StringBuffer replace(int startIndex,int endIndex,String str)

The String is to be  replaced is specified by starting and ending index.Thus substring at startIndex through endIndex-1 is replaced.

CODING:

class replace
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("This is test");
System.out.println("Original String:"+sb);
sb.replace(5,7,"was");

System.out.println("After Replace:"+sb);
}
}

OUTPUT:


No comments:

Post a Comment