java String

String replace() in Java Code With Examples

Table of Contents

Program

public class StringReplace
 {
	public static void main(String[] args)
       {
		String s1 = new String("Welcome To My World");
		System.out.println("1.Replace: "+s1.replace('W','a'));
		String s2 ="My Name Is Samilla";
		String replaceString = s2.replaceAll("a","T");
		System.out.println("2.ReplaceAll: "+replaceString);
		String s3 = new String("My Brother Name Is Kalimulla");
		System.out.println("3.ReplaceFirst: "+s3.replaceFirst("Kalimulla","Mansoor"));
	}
}

Output

	1.Replace: aelcome To My aorld
	2.ReplaceAll: My NTme Is STmillT
	3.ReplaceFirst: My Brother Name Is Mansoor 

Description

Replace:

public String replace​(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing “aa” with “b” in the string “aaa” will result in “ba” rather than “ab”.

Parameters:

target – The sequence of char values to be replaced
replacement – The replacement sequence of char values

Returns:

The resulting string

Since:

1.5

ReplaceAll:

public String replaceAll​(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)

Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher. quote replacement(java.lang.String) to suppress the special meaning of these characters if desired.

Parameters:

regex – the regular expression to which this string is to be matched
replacement – the string to be substituted for each match

Returns:

The resulting String

Throws:

PatternSyntaxException – if the regular expression’s syntax is invalid

Since:

1.4

ReplaceFirst:

public String replaceFirst​(String regex, String replacement)

public String replace first​(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str. replace first(regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceFirst(repl)

Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher. replace first(java.lang.String). Use Matcher. quote, replacement(java.lang.String) to suppress the special meaning of these characters if desired.

Parameters:

regex – the regular expression to which this string is to be matched
replacement – the string to be substituted for the first match

Returns:

The resulting String

Throws:

PatternSyntaxException – if the regular expression’s syntax is invalid

Since:

1.4