Strings In Depth6 / 18
Up next

Replace text

class Main {
    public static void main(String[] args) {
        String s = "a-b-c-d";
        // replace swaps every literal occurrence
        System.out.println(s.replace("-", "_"));
        // replaceFirst takes a regex and changes one match
        System.out.println(s.replaceFirst("-", "_"));
    }
}
What you're solving

replace all, or just the first