Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. String.replace() / replaceAll() / replaceFirst()

String.replace() / replaceAll() / replaceFirst()

Methods for replacing parts of a string with another string. Use replace() for literal string replacement, and replaceAll() or replaceFirst() for regex-based replacement.

Syntax

// Replaces all occurrences of a literal string.
str.replace(CharSequence target, CharSequence replacement);

// Replaces all substrings that match a regular expression.
str.replaceAll(String regex, String replacement);

// Replaces only the first substring that matches a regular expression.
str.replaceFirst(String regex, String replacement);

Method List

MethodDescription
replace(CharSequence target, CharSequence replacement)Returns a new string with all occurrences of target replaced by replacement. Treats the target as a literal string.
replaceAll(String regex, String replacement)Returns a new string with all substrings matching the regular expression regex replaced by replacement.
replaceFirst(String regex, String replacement)Returns a new string with only the first substring matching the regular expression regex replaced by replacement.

Sample Code

// Use replace() to replace all occurrences of a literal string.
String str = "りんごとみかんとりんご";
System.out.println(str.replace("りんご", "ぶどう")); // Prints "ぶどうとみかんとぶどう".

// Remove specific characters by replacing them with an empty string.
String spaced = "H e l l o";
System.out.println(spaced.replace(" ", "")); // Prints "Hello".

// Use replaceAll() to replace matches using a regular expression.
String text = "電話: 090-1234-5678";
System.out.println(text.replaceAll("[0-9]", "*")); // Prints "電話: ***-****-****".

// Collapse multiple spaces into one.
String messy = "Hello   Java   World";
System.out.println(messy.replaceAll("\\s+", " ")); // Prints "Hello Java World".

// Use replaceFirst() to replace only the first match.
String log = "[ERROR] disk full [ERROR] retry";
System.out.println(log.replaceFirst("\\[ERROR\\]", "[WARN]"));
// Prints "[WARN] disk full [ERROR] retry".

// replace() treats the target as a literal, so . and * are not regex metacharacters.
String dotStr = "1.2.3";
System.out.println(dotStr.replace(".", "-")); // Prints "1-2-3".

Notes

replace() operates on literal strings and does not interpret regular expressions. This means characters like . and * are treated as plain characters.

replaceAll() and replaceFirst() support regex-based replacement. If the replacement string contains characters with special meaning in regex — such as backslashes or dollar signs — you must escape them using Matcher.quoteReplacement(). If you call these methods repeatedly in performance-sensitive code, consider using Pattern.compile() to reuse a compiled pattern.

To search within a string, see indexOf() / contains(). To split a string, see split().

If you find any errors or copyright issues, please .