Java 8 – How to replace a String with another String ?

In this article, we will understand with a Java program on how to replace one String (character or sub-string) with another String using Java 1.8 version

Already in one of the previous article, we discussed how to replace a String with another String using earlier versions of Java like 5 or 7, etc.

Replace a String with another String:

  • replace() method of String
    • Returns a string, after replacing all occurrences of character/sub-string with another character/sub-string
  • There are 2 variants of replace() method,
    • replace(char oldChar, char newChar); – Returns a string, after replacing all occurrences of old-character with new-character
    • replace(CharSequence target, CharSequence replacement); – Returns a string, after replacing all occurrences of old-character-sequence (old string) with new-character-sequence (new string)
  • There are 2 more methods which are similar like replace() method,
    • replaceAll(String regex, String replacement); – Returns a string, after replacing all occurrences of invoking-string with replacement string that matches the specified regular expression
    • replaceFirst(String regex, String replacement); – Returns a string, after replacing 1st occurrences of invoking-string with replacement string that matches the specified regular expression

ReplaceStringExample.java

package in.bench.resources.java8.string.methods;

import java.util.stream.Stream;

public class ReplaceStringExample {

	public static void main(String[] args) {

		// 1. test string 1
		String str1 = "Live long";
		System.out.println("Original String :- " + str1);


		// 1.1 replace a single character
		String charReplacement = Stream
				.of(str1)
				.map(str -> str.replace('i', 'o'))
				.findFirst()
				.get();
		System.out.println("Replaced String :- " + charReplacement);


		// 2. test string 2
		String str2 = "Human born with talent";
		System.out.println("\nOriginal String :- " + str2);


		// 2.1 replace a single word/sub-string
		String strReplacement = Stream
				.of(str2)
				.map(str -> str.replace("Human", "Women"))
				.findFirst()
				.get();
		System.out.println("Replaced String :- " + strReplacement);


		// 3. test string 3
		String str3 = "Green India, Clean India";
		System.out.println("\nOriginal String :- " + str3);


		// 3.1 replace a all word/sub-string
		String strReplaceAll = Stream
				.of(str3)
				.map(str -> str.replaceAll("India", "Delhi"))
				.findFirst()
				.get();
		System.out.println("Replaced String :- " + strReplaceAll);


		// 4. test string 4
		String str4 = "Green India, Clean India";
		System.out.println("\nOriginal String :- " + str4);


		// 4.1 replace a all word/sub-string
		String strReplaceFirst = Stream
				.of(str4)
				.map(str -> str.replaceFirst("India", "Delhi"))
				.findFirst()
				.get();
		System.out.println("Replaced String :- " + strReplaceFirst);
	}
}

Output:

Original String :- Live long
Replaced String :- Love long

Original String :- Human born with talent
Replaced String :- Women born with talent

Original String :- Green India, Clean India
Replaced String :- Green Delhi, Clean Delhi

Original String :- Green India, Clean India
Replaced String :- Green Delhi, Clean India

Q) What is the difference between replace() and replaceAll() methods ?

  • replace() method works on character/sub-string
  • replaceAll() method works with regex i.e., regular expression

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - How to split a String based on delimiter ?
Java 8 - How to remove leading and trailing whitespaces in a String ?