.
In Depth Banner
Skip Navigation Links

Select your preferred language

TrimStart and TrimEnd

To remove unneeded white space from the start or end of a line of text.

    /// <summary>

    /// Removes the whitespace from the front of a word list

    /// </summary>

    /// <remarks>Tab characters are not removed because they perform sensible formatting at the beginning of a line.</remarks>

    /// <param name="words">A reference to the word list that should be trimmed</param>

    public void TrimStart(ref WordPosCollection words)

    {

      int firstindex=0;

      if(words.Count==0)

        return;

      if(words[0].WhiteSpace==WhiteSpace.Indent)

        firstindex=1;

      float pos=words[0].PagePos;

      bool needsRePos = false;

      while(words[firstindex].WhiteSpace==WhiteSpace.Space || words[firstindex].WhiteSpace==WhiteSpace.NewLine)

      {

        words.RemoveAt(firstindex);

        needsRePos=true;

        if(words.Count>0)

          words[0].PagePos=pos;

      }

      if(needsRePos)

        RePos(ref words);

    }

 

    /// <summary>

    /// Trims the whitespace from the end of a word list

    /// </summary>

    /// <param name="words">A reference to the word list to be trimmed</param>

    public void TrimEnd(ref WordPosCollection words)

    {

      if(words.Count==0)

        return;

      while(words.Count!=0 && (words.Last().WhiteSpace!=WhiteSpace.None))        

      {

        words.RemoveLast();

      }

    }

 

Return to the article

Copyright © Bob Powell 2000-.  All rights reserved.