# Prob

**URL:** <https://www.ruby-forum.com/t/prob/66794>\
**Category:** Rails\
**Created:** [August 29, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/prob/66794 "2006-08-29T12:06:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [August 29, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/prob/66794/1 "2006-08-29T12:06:17Z")

</div>

Hi,

there is a problem i’ve been trying to solve for a couple of hours now,  
and after some useless googeling and searching around, i haven’t come up  
with anything substansial - i thought the forum might help me.

say i have a string and want to display only the first 10 characters or  
so:

shortstring = “this is a very long string object”[0…10]

# shortstring = "this is a " # which is great

but if i use the same method on a utf8 string, i get some weird  
characters popping in there, sometimes yes, sometimes no. from looking  
around it seems that because every character is two bytes(as apposed to  
1 in regular encoding) there is sometimes a sum of odd/even characters,  
and then the [0…10] doesn’t work correctly, populating wierd  
characters. (same deal goes for the String#slice method)

the final result i need, in essence of this message is this:

“very long string in utf8” to become  
“very lon…”

without weird characters.  
any help, much appreciated.

thanks,  
harp

---

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [August 29, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/prob/66794/2 "2006-08-29T12:44:55Z")

</div>

Have you tried TextHelper#truncate?

> **[ActionView::Helpers::TextHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000505)**
>
> The TextHelper module provides a set of methods for filtering, formatting and transforming strings, which can reduce the amount of inline Ruby code in your views.

---

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [August 29, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/prob/66794/3 "2006-08-29T15:29:16Z")

</div>

harper wrote:

> say i have a string and want to display only the first 10 characters or  
> so:
> 
> shortstring = “this is a very long string object”[0…10]
> 
> # shortstring = "this is a " # which is great
> 
> but if i use the same method on a utf8 string, i get some weird  
> characters popping in there, sometimes yes, sometimes no.

Neither Ruby nor Rails natively supports UTF-8. The built-in string  
functions are assuming single-byte characters, which is why it’s  
returning weird results for the double-byte characters in your UTF-8  
string.

This page on the wiki may help:

[http://wiki.rubyonrails.org/rails/pages/HowToUseUnicodeStrings](http://wiki.rubyonrails.org/rails/pages/HowToUseUnicodeStrings)

Chris
