Replace Characters in Java: Explanation & Practice

Replace parts of a string

Problem summary

Replace all 'a' with '@' in 'Java'.

Starter code

public class Main {
    public static void main(String[] args) {
        String text = "Java";
        
        // Replace and print
    }
}

Expected output and test cases

  • Replace a with @
    J@v@

Hints

  1. Use .replace(old, new) method
  2. Replaces all occurrences
  3. Returns a new string

Related Strings exercises

Practice all Strings exercises · Run this idea in the Java compiler