java tutorial - Java Strings replace() method - java programming - learn java - java basics - java for beginners



Java strings replace

Learn Java - Java tutorial - Java strings replace - Java examples - Java programs

Description

The replace() method — returns In Java a new line, as a result, replacing all occurrences of oldChar, in this line, with newChar, in other words - the method allows to replace the character in the string.

Syntax

Syntax method:

public String replace(char oldChar, char newChar)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • oldChar is an old character;
  • newChar is the new character.

Return value

  • In Java, replace () returns a string that is derived from this string, replacing all occurrences of oldChar with newChar.

Sample Code

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to wikitechy.com");

      System.out.print("Return Value :" );
      System.out.println(Str.replace('t', 'i'));

      System.out.print("Return Value :" );
      System.out.println(Str.replace('y', 'l'));
   }
}
click below button to copy the code. By - java tutorial - team

Output

Return Value :Welcome io wikiiechy.com
Return Value :Welcome to wikitechl.com

Related Searches to Java Strings replace() method