# Special type of string replacement

**URL:** <https://www.ruby-forum.com/t/special-type-of-string-replacement/129231>\
**Category:** Ruby\
**Created:** [February 15, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/special-type-of-string-replacement/129231 "2008-02-15T17:20:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 15, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/special-type-of-string-replacement/129231/1 "2008-02-15T17:20:50Z")

</div>

Hi,

I want to insert the “​” character in between each character of  
a string contained in @user.email. So if the string output were  
“removed\_email\_address@domain.invalid”, the result would be  
“m​e​@​m​i​n​e​.​c​o​m”.

Thanks for the info, - Dave

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 15, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/special-type-of-string-replacement/129231/2 "2008-02-15T17:33:53Z")

</div>

laredotornado wrote:

> Hi,
> 
> I want to insert the “​” character in between each character of  
> a string contained in @user.email. So if the string output were  
> “removed\_email\_address@domain.invalid”, the result would be  
> “m​e​@​m​i​n​e​.​c​o​m”.
> 
> Thanks for the info, - Dave

Something like this will do:  
email = “removed\_email\_address@domain.invalid”  
email[0…-1].gsub(/./){|c| c+“​”}+email[-1…-1]

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 15, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/special-type-of-string-replacement/129231/3 "2008-02-15T17:37:20Z")

</div>

laredotornado wrote:

> Hi,
> 
> I want to insert the “​” character in between each character of  
> a string contained in @user.email. So if the string output were  
> “removed\_email\_address@domain.invalid”, the result would be  
> “m​e​@​m​i​n​e​.​c​o​m”.
> 
> Thanks for the info, - Dave

p “astring”.split("").join(“​”)

regards,

Siep

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [February 15, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/special-type-of-string-replacement/129231/4 "2008-02-15T17:53:16Z")

</div>

Joachim G. wrote:

> email = “removed\_email\_address@domain.invalid”  
> email[0…-1].gsub(/./){|c| c+“​”}+email[-1…-1]

I guess you want that to be email[0…-2] not email[0…-1] (otherwise  
you’ll  
have the last character twice). Anyway, here’s my gsub solution:  
email.gsub(/(.)(?=.)/, ‘\1​’)

HTH,  
Sebastian
