Wednesday, 19 August 2015

Program to replace character from the String .

package com.JavaInterview.Programs;
/* How to get replace character or a string into a string with the given string. String provides replace() * method to replace a specific character or a string which occurs first. replaceAll() method replaces a *&specific character or a string at each occurrence. 
* @author:http://javainterviewprograms.blogspot.in/
*/

public class CharacterReplace {

public static void main(String args[])
{
String str="This is JIP Example String";

System.out.println("Original String : " + str);
//Replace one characters in string with another 
System.out.println("Replace Character 's' with 'x' :"+str.replace('s', 'x'));

//Replace first substring with another
System.out.println("Replace first substring 'is' with 'xy' :"+str.replaceFirst("is", "xy"));

//Replace all substring with another
System.out.println("Replace all substring 'is' with 'pp' :"+str.replaceAll("is", "pp"));
}

}

Output :
Original String : This is JIP Example String
Replace Character 's' with 'x' :Thix ix JIP Example String
Replace first substring 'is' with 'xy' :Thxy is JIP Example String
Replace all substring 'is' with 'pp' :Thpp pp JIP Example String

No comments:

Post a Comment

Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...