# Get a portion of a utf8 encoded string

**URL:** <https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695>\
**Category:** Ruby\
**Created:** [September 16, 2008, 9:57am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695 "2008-09-16T09:57:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 16, 2008, 9:57am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/1 "2008-09-16T09:57:02Z")

</div>

hi all.

ruby 1.8.5 (2006-08-25) [i386-linux]

> > puts ‘hÃ¤llo’  
> > hÃ¤llo  
> > =\> nil
> 
> > puts ‘hÃ¤llo’[0, 2]  
> > hâ–’  
> > =\> nil
> 
> > puts ‘hÃ¤llo’[0, 3]  
> > hÃ¤  
> > =\> nil
> 
> > require ‘iconv’  
> > =\> true
> 
> > puts ‘hÃ¤llo’[0, 3]  
> > hÃ¤  
> > =\> nil
> 
> > puts ‘hÃ¤llo’[0, 2]  
> > hâ–’  
> > =\> nil
> 
> >

The Ã¤ character is probably 2 bytes long, so puts ‘hÃ¤llo’[1, 1] returns  
only a “pice” of the Ã¤ character. Is there a way to make this work?  
‘hÃ¤llo’[0, 2] should return -\> hÃ¤  
and  
‘hÃ¤llo’[0, 3] should return -\> hÃ¤l

thanks

---

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 16, 2008, 10:31am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/2 "2008-09-16T10:31:41Z")

</div>

Hi,

2008/9/16 Marco [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > puts ‘hÃ¤llo’[0, 3]

You can do it using regular expression :

‘hÃ¤llo’.split(//u)[0, 2].to\_s → hÃ¤

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 16, 2008, 10:47am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/3 "2008-09-16T10:47:35Z")

</div>

Heesob P. wrote:

> You can do it using regular expression :
> 
> ‘hÃ¤llo’.split(//u)[0, 2].to\_s -\> hÃ¤

thanks. even if ‘hÃ¤llo’[0, 2] looks better, this works.

---

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 16, 2008, 10:58am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/4 "2008-09-16T10:58:08Z")

</div>

From: S2 [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# thanks. even if ‘hÃ¤llo’[0, 2] looks better

upgrade?

> [RUBY\_VERSION, RUBY\_PATCHLEVEL, RUBY\_REVISION, RUBY\_PLATFORM]  
> =\> [“1.8.7”, 5000, 0, “i686-linux”]

> puts ‘hÃ¤llo’  
> hÃ¤llo  
> =\> nil

> puts ‘hÃ¤llo’[0, 2]  
> hÃ¤  
> =\> nil

> puts ‘hÃ¤llo’[0, 3]  
> hÃ¤l  
> =\> nil

---

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 17, 2008, 6:20am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/5 "2008-09-17T06:20:05Z")

</div>

Heesob P. wrote:

> You can do it using regular expression :
> 
> ‘hÃ¤llo’.split(//u)[0, 2].to\_s -\> hÃ¤

Why make it convoluted when ruby makes it so easy to use regexps:

> > puts ‘hÃ¤llo’[/.{2}/um]  
> > hÃ¤

---

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 18, 2008, 1:44am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/6 "2008-09-18T01:44:38Z")

</div>

On Tue, Sep 16, 2008 at 4:50 AM, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From: S2 [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # thanks. even if ‘hällo’[0, 2] looks better
> 
> upgrade?

It concerns me that people are suggesting using various backwards  
incompatible changes in Ruby 1.8.7

Though 1.8.7 may be a good fallback for 1.9 only libraries that may  
make things work as expected, I hate the idea of writing code that  
works on 1.8.7 but not other versions of Ruby 1.8. At this point, I  
feel like 1.8.6 is still the ‘real’ Ruby 1.8, anyway.

-greg

---

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 18, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/7 "2008-09-18T18:39:44Z")

</div>

On Wed, Sep 17, 2008 at 9:36 PM, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> cmon, greg, what could be better than
> 
> ‘hällo’[0, 2]
> 
> ?
> 
> the change is good.

The change is absolutely good! I did a training session at Lone Star  
Ruby Conference that sung its praises.

What isn’t good is to say “I’ve not yet updated my code for Ruby 1.9”,  
so it works on Ruby 1.8.7 only.

My point is that if people really want to use Ruby 1.9 features for  
anything but experimentation, they’d do more good by actually using  
1.9, not an intermediate release that left most people confused.

If you are writing Ruby 1.8.7 specific code, your code may not run on  
Ruby 1.9, for example, the code above will blow up on 1.9 unless the  
encoding is properly set.

And your code _definitely_ won’t work on Ruby 1.8.x aside from 1.8.7

So if you’re okay locking to a single point release, that’s fine. But  
I think it’d help ruby-core a lot more for you to use Ruby 1.9 and  
help them iron out issues, and it’d help the Ruby community a lot more  
for you to choose whether you are supporting 1.8.x, 1.9.x or both, but  
not something that is neither (1.8.7)

-greg

---

<div class="post-metadata">

**Author:** ![marco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@marco](https://www.ruby-forum.com/u/marco)\
**Post date:** [September 18, 2008, 4:10am UTC](https://www.ruby-forum.com/t/get-a-portion-of-a-utf8-encoded-string/148695/8 "2008-09-18T04:10:29Z")

</div>

From: Gregory B. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# On Tue, Sep 16, 2008 at 4:50 AM, Peña, Botp

# [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

# \> From: S2 [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# \> # thanks. even if ‘hällo’[0, 2] looks better

# \> upgrade?

# It concerns me that people are suggesting using various backwards

# incompatible changes in Ruby 1.8.7

# Though 1.8.7 may be a good fallback for 1.9 only libraries that may

# make things work as expected, I hate the idea of writing code that

# works on 1.8.7 but not other versions of Ruby 1.8. At this point, I

# feel like 1.8.6 is still the ‘real’ Ruby 1.8, anyway.

cmon, greg, what could be better than

‘hällo’[0, 2]

?

the change is good.

kind regards -botp
