Word wrap line break code and algorithm for c#

Discussion in 'ASP .Net' started by Jason Coyne Gaijin42, Apr 8, 2004.

  1. Here is a little word wrap function I whipped up today. It suports a
    variable line length, and text inside an html tag is not counted.
    (This is so you can display text that contains HTML, without the tag
    itself counting against the line length, since the tag is not
    displayed to the user)

    I hope you find it useful.

    Sorry about the formatting, usenet alwasy messes up my code. When you
    paste this into Visual Studio, it should fix the indenting for you
    automagically.

    public static string[] Wrap(string text, int maxLength)
    {
    text = text.Replace("\n", " ");
    text = text.Replace("\r", " ");
    text = text.Replace(".", ". ");
    text = text.Replace(">", "> ");
    text = text.Replace("\t", " ");
    text = text.Replace(",", ", ");
    text = text.Replace(";", "; ");
    text = text.Replace("<br>", " ");
    text = text.Replace(" ", " ");

    string[] Words = text.Split(' ');
    int currentLineLength = 0;
    ArrayList Lines = new ArrayList(text.Length / maxLength);
    string currentLine = "";
    bool InTag = false;

    foreach (string currentWord in Words)
    {
    //ignore html
    if (currentWord.Length > 0)
    {

    if (currentWord.Substring(0,1) == "<")
    InTag = true;

    if (InTag)
    {
    //handle filenames inside html tags
    if (currentLine.EndsWith("."))
    {
    currentLine += currentWord;
    }
    else
    currentLine += " " + currentWord;

    if (currentWord.IndexOf(">") > -1)
    InTag = false;
    }
    else
    {
    if (currentLineLength + currentWord.Length + 1 < maxLength)
    {
    currentLine += " " + currentWord;
    currentLineLength += (currentWord.Length + 1);
    }
    else
    {
    Lines.Add(currentLine);
    currentLine = currentWord;
    currentLineLength = currentWord.Length;
    }
    }
    }

    }
    if (currentLine != "")
    Lines.Add(currentLine);

    string[] textLinesStr = new string[Lines.Count];
    Lines.CopyTo(textLinesStr, 0);
    return textLinesStr;


    }
     
    Jason Coyne Gaijin42, Apr 8, 2004
    #1
    1. Advertisements

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. Mark A. Sam

    Content Holder and word wrap

    Mark A. Sam, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    495
    Mark A. Sam
    Mar 28, 2006
  2. CRON
    Replies:
    10
    Views:
    22,269
  3. name

    Line/word wrap program.

    name, Aug 19, 2004, in forum: C Programming
    Replies:
    11
    Views:
    770
    CBFalconer
    Dec 9, 2004
  4. Frost
    Replies:
    8
    Views:
    843
    Vladimir S. Oka
    Feb 10, 2006
  5. Replies:
    9
    Views:
    766
  6. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    1,036
    Chronic Philharmonic
    May 10, 2008
  7. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    453
    Art Werschulz
    Sep 22, 2003
  8. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    445
    Anno Siegel
    Sep 25, 2003
Loading...