# Html formatted string -displaying only upto specific length

**URL:** <https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/197118>\
**Category:** Ruby\
**Created:** [January 6, 2010, 9:36am UTC](https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/197118 "2010-01-06T09:36:59Z")\
**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:36am UTC](https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/197118/1 "2010-01-06T09:36:59Z")

</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 6, 2010, 7:06pm UTC](https://www.ruby-forum.com/t/html-formatted-string-displaying-only-upto-specific-length/197118/2 "2010-01-06T19:06:42Z")

</div>

Subashini K. wrote:

> 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.

You’ll need to parse the HTML to know where the tags begin and end. Use  
a library such as Nokogiri for that.

You could do a quick-and-dirty approach using regular expressions, but  
it may not be as reliable.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
