Saturday, March 20, 2010

Paragraph Trim function

 

 Trim your paragraph to limited number of words. A very useful function for designing text based websites like news or articles archives or list.

 

protected string paraTrim(string Input, int no_of_words)

    {

 

        // split input string into words (max 21...last words go in last element)

        String[] Words = Input.Split(new char[] { ' ' }, no_of_words);

 

        // if we reach maximum words, replace last words with elipse

        if (Words.Length == no_of_words)

            Words[no_of_words - 1] = "...";

        else

            return Input;  // nothing to do

 

        // build new output string

        String Output = String.Join(" ", Words);

        return Output;

 

    }


No comments: