www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string find and replace

reply "Iain" <staffell gmail.com> writes:
Forgive me if I am missing something obvious, but is there a 
simple option for finding all instances of a particular character 
in a string or char[] and replacing them with another character?

I can do this with std.regex, but it seems overkill, when all I 
want is the equivalent of PHP's str_replace() function.

Many thanks!
May 03 2012
parent reply "Iain" <staffell gmail.com> writes:
On Thursday, 3 May 2012 at 14:22:57 UTC, Iain wrote:
 Forgive me if I am missing something obvious, but is there a 
 simple option for finding all instances of a particular 
 character in a string or char[] and replacing them with another 
 character?

 I can do this with std.regex, but it seems overkill, when all I 
 want is the equivalent of PHP's str_replace() function.

 Many thanks!
Apologies, after half an hour searching, I post, then five minutes later figure it out. myString = replace(myString, to, from); // from std.array
May 03 2012
parent reply Ary Manzana <ary esperanto.org.ar> writes:
On 5/3/12 9:30 PM, Iain wrote:
 On Thursday, 3 May 2012 at 14:22:57 UTC, Iain wrote:
 Forgive me if I am missing something obvious, but is there a simple
 option for finding all instances of a particular character in a string
 or char[] and replacing them with another character?

 I can do this with std.regex, but it seems overkill, when all I want
 is the equivalent of PHP's str_replace() function.

 Many thanks!
Apologies, after half an hour searching, I post, then five minutes later figure it out. myString = replace(myString, to, from); // from std.array
Note that you can also do: myString = myString.replace(to, from) I'd point you to the reference on the official page (UFCS: unified function call syntax), but I can't find it...
May 03 2012
parent Ary Manzana <ary esperanto.org.ar> writes:
On 5/3/12 11:01 PM, Ary Manzana wrote:
 On 5/3/12 9:30 PM, Iain wrote:
 On Thursday, 3 May 2012 at 14:22:57 UTC, Iain wrote:
 Forgive me if I am missing something obvious, but is there a simple
 option for finding all instances of a particular character in a string
 or char[] and replacing them with another character?

 I can do this with std.regex, but it seems overkill, when all I want
 is the equivalent of PHP's str_replace() function.

 Many thanks!
Apologies, after half an hour searching, I post, then five minutes later figure it out. myString = replace(myString, to, from); // from std.array
Note that you can also do: myString = myString.replace(to, from) I'd point you to the reference on the official page (UFCS: unified function call syntax), but I can't find it...
and it should be replace(from, to)
May 03 2012