Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to replace specific characters in a string?

Hi.
I've got a string, e.g. string s = "ahsdfhldfasfwi";
I'd like to replace all of the 'f's in the string by 'o'.
What is the most efficient way to implement this in standard c++?
Thanks.
[256 byte] By [WXY595] at [2007-11-11 10:20:20]
# 1 Re: How to replace specific characters in a string?
Hi,
use std::replace(s.begin(),s.end(),'f','o');
(I haven't tested it - I leave it to you as an excercise ;-)
You need to include the <algorithm> header.
Cheers,
D
drkybelk at 2007-11-11 20:59:02 >
# 2 Re: How to replace specific characters in a string?
More details on string algorithms can be found here: http://www.dev-archive.com/getHelpOn/10MinuteSolution/16972
Danny at 2007-11-11 21:00:02 >