23.11.2017 Views

java_tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Java<br />

Java – String replace() Method<br />

Description<br />

This method returns a new string resulting from replacing all occurrences of oldChar in<br />

this string with newChar.<br />

Syntax<br />

Here is the syntax of this method:<br />

public String replace(char oldChar, char newChar)<br />

Parameters<br />

Here is the detail of parameters:<br />

<br />

<br />

oldChar -- the old character.<br />

newChar -- the new character.<br />

Return Value<br />

<br />

Example<br />

It returns a string derived from this string by replacing every occurrence of oldChar<br />

with newChar.<br />

import <strong>java</strong>.io.*;<br />

public class Test{<br />

public static void main(String args[]){<br />

String Str = new String("Welcome to Tutorialspoint.com");<br />

System.out.print("Return Value :" );<br />

System.out.println(Str.replace('o', 'T'));<br />

}<br />

}<br />

System.out.print("Return Value :" );<br />

System.out.println(Str.replace('l', 'D'));<br />

This will produce the following result:<br />

Return Value :WelcTme tT TutTrialspTint.cTm<br />

Return Value :WeDcome to TutoriaDspoint.com<br />

173

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!