How to replace all occurrences of a character in string?

How to Replace All Occurrences of a Character in a String

Replacing all occurrences of a character in a string is a common operation in many programming languages. Here’s how you can achieve this in a variety of languages, including Python, Java, JavaScript, and C++.

Python

In Python, you can use the built-in str.replace() method to replace all occurrences of a character in a string.

# Using str.replace() method original_string = "hello world" modified_string = original_string.replace('l', 'x') print(modified_string) # Output: hexxo worxd

Java

In Java, you can use the String.replace() method, which replaces all occurrences of a specified character with another character.

public class Main { public static void main(String[] args) { String originalString = "hello world"; String modifiedString = originalString.replace('l', 'x'); System.out.println(modifiedString); // Output: hexxo worxd } }

JavaScript

In JavaScript, you can use the String.prototype.replace() method along with a global regular expression to replace all occurrences of a character.

// Using String.prototype.replace() with a global regular expression let originalString = "hello world"; let modifiedString = originalString.replace(/l/g, 'x'); console.log(modifiedString); // Output: hexxo worxd

C++

In C++, you can replace characters in a string using the std::replace algorithm from the <algorithm> library.

#include <iostream> #include <string> #include <algorithm> int main() { std::string originalString = "hello world"; std::replace(originalString.begin(), originalString.end(), 'l', 'x'); std::cout << originalString << std::endl; // Output: hexxo worxd return 0; }

Summary

  • Python: Use str.replace('old_char', 'new_char').
  • Java: Use String.replace('old_char', 'new_char').
  • JavaScript: Use String.prototype.replace(/old_char/g, 'new_char').
  • C++: Use std::replace from the <algorithm> library.

Each method is idiomatic to the language it is used in and provides an efficient way to replace characters in a string. For more in-depth knowledge and practical examples on programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides comprehensive courses on essential coding and interview techniques.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How long are Google interviews?
Does NVIDIA give bonuses?
Is data engineering code heavy?
Which is better CS or CSE or CE?
How to understand edge computing concepts for software interviews?
Why do I want to work for Spotify?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.