# Idiom of removing a particular character from a String?

**URL:** <https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857>\
**Category:** Ruby\
**Created:** [August 14, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857 "2008-08-14T16:24:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 14, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/1 "2008-08-14T16:24:49Z")

</div>

Hi all,

Is there any particular idiom for removing a particular character from a  
string _by index_ and returning the resulting string? The following  
makes my old eyes sore:

rem = str.clone  
rem[i] = “”  
rem

as does this:

rem = str.scan(/./)  
rem.delete\_at(i)  
rem.to\_s

It seems there should be some more idiomatic way to do this, something  
like str.annihilate\_char\_at\_index\_possibly\_without\_a\_bang(i), read as  
String#delete\_at(i) and String#delete\_at!(i). Of course these methods  
don’t exist, and “”.methods doesn’t turn up anything interesting. So  
what is a _decent_ way of performing this?

Lorenzo

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 14, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/2 "2008-08-14T16:43:35Z")

</div>

On Thu, Aug 14, 2008 at 11:21:10PM +0900, Lorenzo E. Danielsson wrote:

> rem = str.scan(/./)  
> rem.delete\_at(i)  
> rem.to\_s
> 
> It seems there should be some more idiomatic way to do this, something  
> like str.annihilate\_char\_at\_index\_possibly\_without\_a\_bang(i), read as  
> String#delete\_at(i) and String#delete\_at!(i). Of course these methods  
> don’t exist, and “”.methods doesn’t turn up anything interesting. So  
> what is a _decent_ way of performing this?

str[index..index] = “”

> Lorenzo  
> –Greg

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 14, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/3 "2008-08-14T16:55:54Z")

</div>

On Thu, Aug 14, 2008 at 9:21 AM, Lorenzo E. Danielsson  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> as does this:
> 
> rem = str.scan(/./)  
> rem.delete\_at(i)  
> rem.to\_s
> 
> It seems there should be some more idiomatic way to do this, something  
> like str.annihilate\_char\_at\_index\_possibly\_without\_a\_bang(i), read as  
> String#delete\_at(i) and String#delete\_at!(i). Of course these methods  
> don’t exist, and “”.methods doesn’t turn up anything interesting. So  
> what is a _decent_ way of performing this?

irb(main):022:0\> rem = “foobar”  
=\> “foobar”  
irb(main):023:0\> rem.slice!(3).chr  
=\> “b”  
irb(main):024:0\> rem  
=\> “fooar”

-Michael

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 14, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/4 "2008-08-14T19:06:06Z")

</div>

On Thu, 2008-08-14 at 23:40 +0900, Gregory S. wrote:

> > 
> 
> str[index..index] = “”  
> So:  
> str = “hello”  
> rem = str.clone  
> rem[2,1] = “” # =\> “helo”  
> rem

Sorry, but that doesn’t look any cleaner than my solution. I was looking  
for something like:

class String  
def delete\_at(i)  
str = self.clone  
str[i] = “”  
end  
end

str = “hello”  
rem = str.delete\_at(2) # =\> “helo”

That is, original string does not get tampered with. Resulting string is  
original string minus exactly one character, at the specified index.

The above works, but I was hoping to avoid that by using some neat  
idiom. This is not really a problem, but 99.x % of the time, Ruby does  
things in a way that are so cool that I tend to get lazy and expect it  
to always behave that way. The few times it doesn’t, it comes as a  
shock.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 14, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/5 "2008-08-14T19:13:09Z")

</div>

On Thu, 2008-08-14 at 23:52 +0900, Michael L. wrote:

> > rem  
> > don’t exist, and “”.methods doesn’t turn up anything interesting. So  
> > what is a _decent_ way of performing this?
> 
> irb(main):022:0\> rem = “foobar”  
> =\> “foobar”  
> irb(main):023:0\> rem.slice!(3).chr  
> =\> “b”  
> irb(main):024:0\> rem  
> =\> “fooar”

The “problem” is that slice does not return the resulting string but  
rather the sliced portion. This is of course not a problem in itself, it  
is rather the expected behavior of slice. But, in my particular case  
that _still_ leads to:

str = “hello”  
rem = str.clone  
rem.slice!(2)  
rem # =\> “helo”

Whereas I would have wanted:

str = “hello”  
rem = str.slice\_and\_return\_everything\_but\_the\_sliced\_part(2)

This is partly a matter of laziness on my part, that I prefer to type  
two lines instead of four. But, it is also a matter that it kind of  
breaks the POLS, at least for me. I have grown accustomed to being able  
to perform:

result = original.some\_method(args)

and have the result be some mutilated form of original, with original  
being left intact. So when String#delete\_at(index) doesn’t work, it  
feels like it _should_ have worked..

Anyways, nothing worth losing sleep over..

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 14, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/6 "2008-08-14T19:23:08Z")

</div>

On Aug 14, 2008, at 8:21 AM, Lorenzo E. Danielsson wrote:

> what is a _decent_ way of performing this?
> 
> Lorenzo

don’t do it by index

string.tr! chars, ‘’

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 15, 2008, 5:01am UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/7 "2008-08-15T05:01:50Z")

</div>

On Thursday 14 August 2008 11:59:51 Lorenzo E. Danielsson wrote:

> So:  
> str = “hello”  
> rem = str.clone  
> rem[2,1] = “” # =\> “helo”  
> rem

For what it’s worth, I’ve been using Object#tap (1.8.7 or higher) in  
situations like these:

str = “hello”  
str.clone.tap { |rem|  
rem[2,1] = ‘’  
}

Ordinarily, I wouldn’t mention it, but you were asking for idiomatic  
things,  
and I like that idiom.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 14, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/8 "2008-08-14T19:45:28Z")

</div>

On Aug 14, 9:21 am, “Lorenzo E. Danielsson”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> as does this:
> 
> Lorenzo

Lorenzo,

It seems reading comprehension isn’t a strong suit for ruby-talk  
today.

As far as I know, there is no such method. You’re probably stuck with  
reopening String and adding it yourself.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 15, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/9 "2008-08-15T15:46:53Z")

</div>

On Fri, 2008-08-15 at 11:56 +0900, David M. wrote:

> str = “hello”  
> str.clone.tap { |rem|  
> rem[2,1] = ‘’  
> }
> 
> Ordinarily, I wouldn’t mention it, but you were asking for idiomatic things,  
> and I like that idiom.

Agreed, Object#tap looks very nice. Thanks for the info.

Lorenzo

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 15, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/10 "2008-08-15T15:04:06Z")

</div>

2008/8/14 Lorenzo E. Danielsson [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Thu, 2008-08-14 at 23:52 +0900, Michael L. wrote:
> 
> > On Thu, Aug 14, 2008 at 9:21 AM, Lorenzo E. Danielsson

> Whereas I would have wanted:
> 
> and have the result be some mutilated form of original, with original  
> being left intact. So when String#delete\_at(index) doesn’t work, it  
> feels like it _should_ have worked..
> 
> Anyways, nothing worth losing sleep over..

If you want to do it on one line you could do this:

irb(main):011:0\> s = “abcdef”  
=\> “abcdef”  
irb(main):012:0\> 5.times {|i| puts s.sub /\A(.{#{i}})./, ‘\1’ }  
bcdef  
acdef  
abdef  
abcef  
abcdf  
=\> 5  
irb(main):013:0\>

Granted, it’s not too idiomatic…

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 16, 2008, 4:12am UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/11 "2008-08-16T04:12:31Z")

</div>

Lorenzo E. Danielsson wrote:

> Hi all,
> 
> Is there any particular idiom for removing a particular character from a  
> string _by index_ and returning the resulting string?

str[0…index]+str[index+1..-1]

NB: this won’t delete the char from str but only return a new string  
without the character at index.

regards  
Stefan R. (“apeiros”)

---

<div class="post-metadata">

**Author:** ![Lorenzo\_ESDanielss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f6c823/32.png) [@Lorenzo\_ESDanielss](https://www.ruby-forum.com/u/Lorenzo_ESDanielss)\
**Post date:** [August 16, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/idiom-of-removing-a-particular-character-from-a-string/145857/12 "2008-08-16T12:45:58Z")

</div>

Lorenzo E. Danielsson wrote:

> as does this:
> 
> Lorenzo

There have been some good solutions posted already. However, I don’t  
think I’d want to be repeating them many times (or at all) in my code.  
Whichever solution you use, it’s probably best to hide it away in the  
String class somewhere, else your code will suddenly start looking…  
perl-like.

This seems sensible to me. If you’re on 1.8.7 or 1.9 (I am not), you  
can also use the tap method.

class String  
def rem(idx)  
c = clone  
c[idx..idx] = “”  
c  
end

def rem!(idx)  
self[idx..idx] = “”  
self  
end  
end

If you’re on 1.8.7, 1.9 or feel like implementing it yourself, you can  
use the tap method. Using the tap method seems cleaner.

unless Object.methods.include? “tap”  
class Object  
def tap  
yield self  
self  
end  
end  
end

class String  
def rem(idx)  
clone.tap {|t| t[idx..idx] = “” }  
end

def rem!(idx)  
tap {|t| t[idx..idx] = “” }  
end  
end
