String Replace Is Literal Not Regex

String.replace treats the target as literal text while replaceAll compiles a regex, so a plain dot replaces itself or swallows every character.

JavaData Integrity

Code

snippet
import java.util.regex.Pattern;

public class DotReplacer {

    public static void main(String[] args) {
        String config = "app.config.v2.backup";

        // String.replace 是 literal 替换:点号不是正则元字符
        System.out.println(config.replace(".", "_"));
        System.out.println(config.replace('.', '_'));

        // String.replaceAll 才是 regex:点号匹配任意字符
        System.out.println(config.replaceAll(".", "_"));

        // 想按 regex 替换字面点号:转义或 quote
        System.out.println(config.replaceAll("\\.", "_"));
        System.out.println(config.replaceAll(Pattern.quote("."), "_"));
    }
}

How the snippet works

The moving parts are the class DotReplacer and the function main.

What to watch for

  • Nothing arrives from a registry; the language ships every API used.

Using it

Entry signature: public static void main(String[] args)

Key statements

String config = "app.config.v2.backup";

Interactive Tools for This Topic

FAQ

How do you run String Replace Is Literal Not Regex?
Run it as a complete program — compile or interpret the file directly; there is nothing to install first.