Wrap the text in div tag!

Discussion in 'ASP .Net' started by Santel, Aug 11, 2007.

  1. Santel

    Santel Guest

    Hi, I would like to wrap the text in div tag according to its width.
    The text don't have any space. Anyone please suggest me how to do this.
     
    Santel, Aug 11, 2007
    #1
    1. Advertising

  2. Hi there,

    Unfortunatelly, HTML related solutions like <wbr/> (not in HTML spec) tag or
    word-break css attribute (IE only) are partially working for some browsers
    only. But don't worry, there's a generic resolution by inserting an invisible
    span inside long words (my example works for plain text with encoded
    characters, if you want to break long words in HTML formatted text it's gonna
    be harder):

    using System.Text.RegularExpressions;
    using System;

    public static class HtmlUtils
    {
    public static string BreakLongWords(string input, int maxWordLength)
    {
    // anonymous method can be used in this case
    MatchEvaluator evaluator = delegate(Match match)
    {
    string text = match.Value;
    int length = text.Length;

    // this is for html encoded characters that
    // are present in the plain text
    int startIndex = text.LastIndexOf('&',
    length - 1, Math.Min(7, length));

    return startIndex != -1 ?
    text.Insert(startIndex, WordBreak) :
    text + WordBreak;
    };

    string pattern = string.Format(@"[^\s-]{{{0}}}", maxWordLength);

    return Regex.Replace(input, pattern, evaluator);
    }

    private const string WordBreak =
    "<span style=\"font-size:0px;color:transparent;\"> </span>";

    }

    usage:

    <div runat="server" style="width: 200px; border: solid 1px black" id="myDiv">
    </div>

    and the code behind/beside:
    myDiv.InnerHtml = HtmlUtils.BreakLongWords(
    "thisIsVeryLongTextWhichWillBeBrokenIntoSmallerPieces", 30);

    You should be fine from this point on.

    HTH
    --
    Milosz


    "Santel" wrote:

    > Hi, I would like to wrap the text in div tag according to its width.
    > The text don't have any space. Anyone please suggest me how to do this.
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Aug 12, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santel

    Wrap the text in div tag

    Santel, Aug 11, 2007, in forum: HTML
    Replies:
    1
    Views:
    792
    Jukka K. Korpela
    Aug 11, 2007
  2. M Wells
    Replies:
    0
    Views:
    141
    M Wells
    Oct 6, 2004
  3. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    234
    Art Werschulz
    Sep 22, 2003
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    249
    Anno Siegel
    Sep 25, 2003
  5. visu
    Replies:
    4
    Views:
    302
Loading...

Share This Page