Java Data Type Tutorial - 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.

/*from   w  ww  .  j a v a  2s . co  m*/
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.