# Html formatted string & displaying only upto specific length

**URL:** <https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/181143>\
**Category:** Rails\
**Created:** [January 6, 2010, 9:35am UTC](https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/181143 "2010-01-06T09:35:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [January 6, 2010, 9:35am UTC](https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/181143/1 "2010-01-06T09:35:50Z")

</div>

Hi

I have a string

str=“

Hgenrik  
Lundqvist,ggh nv gnm h fsgfb  
gthe preternaturally handsome Swedish goalie for the New  
York Rangers, may have been named one of test from Neskdufluh on  
december a 2009

”

i want to display this with the html formatting as mentioned above in  
the string.  
But i want to display only first 15 charatcer.ie 15 characters inclusive  
of spaces and other special charac  
that is , i want to display HgenrikLundqvis .  
But it should be display with all the html formatting as it is in the  
string “str”.  
please tell me how to display it like this.

---

<div class="post-metadata">

**Author:** ![subashinikumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@subashinikumar](https://www.ruby-forum.com/u/subashinikumar)\
**Post date:** [January 7, 2010, 7:18pm UTC](https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/181143/2 "2010-01-07T19:18:27Z")

</div>

On Jan 6, 3:35Â am, Subashini K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i want to display this with the html formatting as mentioned above in  
> the string.  
> But i want to display only first 15 charatcer.ie 15 characters inclusive  
> of spaces and other special charac  
> that is , Â i want to display Â HgenrikLundqvis .  
> But it should be display with all the html formatting as it is in the  
> string “str”.  
> please tell me how to display it like this.

I don’t think what you’re looking for is even possible in general:  
you’d need to first parse the entire HTML string, find “15 characters”  
and then put the formatting back on. That’s not even including the  
possibility of devious stuff like this:

A string with hidden bits.

The first 15 _visible_ characters here are “A string with .”, but  
unless you’re parsing the CSS as well, you’d end up producing “A  
string with h”.

Finally, fixed character truncation can be tricky; does the client  
_really_ want to see “Foo B. Baz Association” truncated to 15  
characters? 🙂

–Matt J.
