Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[4646] Custom Word Wrapper

Last post 02-26-2008 7:24 AM by quantum_cty. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4646] Custom Word Wrapper

    This thread is for discussions of Custom Word Wrapper.

    • Post Points: 15
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 06-08-2005 7:49 PM In reply to

    • ssbkt
    • Not Ranked
    • Joined on 06-08-2005
    • New Member
    • Points 5

    Thank you and one correction.

    Thank you very much for your custom word wrapper class, saved me a bunch of time.

    I found one problem with it.  If you have a word that by itself is longer then the maxWidth the BreakLines method goes into an infinate loop.  This is how I fixed it, you might have a better idea:
    I added the else if (textWords[wordIndex].Length >= maxWidth) condition.

    private static string[] BreakLines(string originalText, int maxWidth) {
               string[] textWords = originalText.Split(' ');
               int wordIndex = 0;
               string tmpLine = "";
               ArrayList textLines = new ArrayList();
               
               while (wordIndex < textWords.Length) {
                   if (textWords[wordIndex] == "") {
                       wordIndex++;
                   } else {
                       string backupLine = tmpLine;
                       if (tmpLine == "") {
                           tmpLine = textWords[wordIndex];
                       } else {
                           tmpLine = tmpLine + " " + textWords[wordIndex];
                       }
                       
                       if (tmpLine.Length <= maxWidth)
                       {
                           wordIndex++;
                           // If our line is still small enough, but we don't have anymore words
                           // add the line to the collection
                           if (wordIndex == textWords.Length)
                           {
                               textLines.Add(tmpLine);
                           }
                       }
                        else if (textWords[wordIndex].Length >= maxWidth)
                       {
                           wordIndex++;
                           textLines.Add(tmpLine);
                       }

                       else
                       {
                           // Our line is too long, add the previous line to the collection
                           // and reset the line, the word causing the 'overflow' will be
                           // the first word of the new line
                           textLines.Add(backupLine);
                           tmpLine = "";
                       }
                   }
               }
               string[] textLinesStr = new string[textLines.Count];
               textLines.CopyTo(textLinesStr, 0);
               return textLinesStr;
           } /* BreakLines */
    • Post Points: 0
  • 02-26-2008 7:24 AM In reply to

    Re: [4646] Custom Word Wrapper - Improvement

    Thank you for the code. I found that whenever there is words that are longer than max width, it causes bugs. Therefore, I have done the following correction to enable the words to be splited into seperate lines:

    private static string[] BreakLines(string originalText, int maxWidth) {

    string[] textWords;

    textWords= originalText.Split(' ');

    // Handler first string longer than maxwidth scenario

    if (textWords.Length == 1 & textWords[0].Length > maxWidth)

    {

    int remainder=textWords[0].Length % maxWidth;

    int lnBreaked = textWords[0].Length / maxWidth;

    if (remainder >0)

    lnBreaked = lnBreaked + 1;

    textWords = new string[lnBreaked];

    for (int x = 0; x < textWords.Length ; x++)

    {

    int curIdx = x * maxWidth + maxWidth +1;

    if (curIdx <= originalText.Length)

    textWords[x] = originalText.Substring(x * maxWidth, maxWidth);

    else

    textWords[x] = originalText.Substring(x * maxWidth, originalText.Length - (x * maxWidth));

    }

    }

     

    int wordIndex = 0;

    string tmpLine = "";

    ArrayList textLines = new ArrayList();

     

    while (wordIndex < textWords.Length){

    if (textWords[wordIndex] == "") { wordIndex++; }

    else

    {

    string backupLine = tmpLine;

    if (tmpLine == "") { tmpLine = textWords[wordIndex]; }

    else{ tmpLine = tmpLine + " " + textWords[wordIndex]; }

    if (tmpLine.Length <= maxWidth)

    {

    wordIndex++;

    // If our line is still small enough, but we don't have anymore words

    // add the line to the collection

    if (wordIndex == textWords.Length) { textLines.Add(tmpLine); }

    }

    else

    {

    // If current word is longer than maxwidth

    if (tmpLine.Contains(" ") == false && tmpLine.Length > maxWidth)

    {

    int remainder=tmpLine.Length % maxWidth;

    int lnBreaked = tmpLine.Length / maxWidth;

    if (remainder > 0)

    lnBreaked = lnBreaked + 1;

    string[] strTmp = new String[lnBreaked];

    for (int x = 0; x < strTmp.Length; x++)

    {

    int curIdx = x * maxWidth + maxWidth + 1;

    if (curIdx <= tmpLine.Length)

    {

    strTmp[x] = tmpLine.Substring(x * maxWidth, maxWidth);

    }

    else

    {

    strTmp[x] = tmpLine.Substring(x * maxWidth, tmpLine.Length - (x * maxWidth));

    }

    }

    foreach (string s in strTmp)

    {

    textLines.Add(s);

    }

    wordIndex++;

    tmpLine = "";

    }

    else

    {

    // Our line is too long, add the previous line to the collection

    // and reset the line, the word causing the 'overflow' will be

    // the first word of the new line

    textLines.Add(backupLine);

    tmpLine = "";

    }

    }

    }

    }

    string[] textLinesStr = new string[textLines.Count];

    textLines.CopyTo(textLinesStr, 0);

    return textLinesStr;

    } /* BreakLines */

    • Post Points: 5
Page 1 of 1 (3 items)