# Help: Matching last character of a string!

**URL:** <https://www.ruby-forum.com/t/help-matching-last-character-of-a-string/111259>\
**Category:** Ruby\
**Created:** [August 4, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/help-matching-last-character-of-a-string/111259 "2007-08-04T14:46:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![astroruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@astroruby](https://www.ruby-forum.com/u/astroruby)\
**Post date:** [August 4, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/help-matching-last-character-of-a-string/111259/1 "2007-08-04T14:46:24Z")

</div>

Hi,

I am trying to replace the last character of a string with  
another character:

x = “{Hello {hello}}”  
x.sub!(/}$/, “]”)  
puts x

With Ruby 1.8.5 (on Windows) this gives (as expected)  
{Hello {hello}]

With Ruby 1.8.1 (on Linux) I get (surprisingly)  
{Hello {hello}}

Who knows what is going on? Can’t believe a fundamental bug like  
this was still in 1.8.1 …

Cheers  
Uwe

---

<div class="post-metadata">

**Author:** ![astroruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@astroruby](https://www.ruby-forum.com/u/astroruby)\
**Post date:** [August 4, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/help-matching-last-character-of-a-string/111259/2 "2007-08-04T17:30:38Z")

</div>

Uwe Lammers wrote:

> Who knows what is going on? Can’t believe a fundamental bug like  
> this was still in 1.8.1 …

Hi Uwe,

No idea what/why, but at least I can tell you  
that it works as expected on 1.8.4 under Linux.

HM
