A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/regular_replaceall.htm below:

How to replace all occurring of a string in Java

How to replace all occurring of a string in Java Problem Description

How to replace all occurring of a string?

Solution

Following example demonstrates how to replace all occurrence of a String in a String using replaceAll() method of Matcher class.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
   public static void main(String args[]) {
      Pattern p = Pattern.compile("hello");
      String instring = "hello hello hello.";
      System.out.println("initial String: "+ instring);
      Matcher m = p.matcher(instring);
      String tmp = m.replaceAll("Java");
      System.out.println("String after replacing 1st Match: "+tmp);
   }
}
Result

The above code sample will produce the following result.

initial String: hello hello hello.
String after replacing 1st Match: Java Java Java.

java_regular_exp.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4