Java String.replace(CharSequence target, CharSequence replacement)

Syntax

String.replace(CharSequence target, CharSequence replacement) has the following syntax.

public String replace(CharSequence target,  CharSequence replacement)

Example

In the following code shows how to use String.replace(CharSequence target, CharSequence replacement) method.


//  w  ww  . jav  a 2 s .c  om
public class Main {

  public static void main(String[] args) {
  
    String str = "java2s.com";
    System.out.println("string = " + str);

    CharSequence s1 = "j";
    CharSequence s2 = "b";
    String replaceStr = str.replace(s1, s2);

    System.out.println("new string = " + replaceStr);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable