Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / C#
Tip/Trick

Wrap a string to a given width

Rate me:
Please Sign up or sign in to vote.
3.86/5 (4 votes)
1 Mar 2010CPOL 25.5K   2   11
Do you want to wrap a string to a desired width for printing or other purposes, then here is a small snippet.public static List StringWrap(string srcStr, int maxWidth) { List lstLines = new List(); int spcCount = 0; if...
Do you want to wrap a string to a desired width for printing or other purposes, then here is a small snippet.

<code>
public static List<string> StringWrap(string srcStr, int maxWidth)
        {
            List<string> lstLines = new List<string>();
            int spcCount = 0;

            if (!string.IsNullOrEmpty(srcStr))
            {

                string[] Lines = srcStr.Split(new char[] { '\n', '\r' });
                foreach (string Line in Lines)
                {
                    string[] Words = Line.Split(' ');
                    string curLine = "";
                    foreach (var word in Words)
                    {
                        spcCount = (curLine.Length > 0 ? 1 : 0);
                        if (curLine.Length + word.Length + spcCount > maxWidth && !string.IsNullOrEmpty(curLine))
                        {

                            lstLines.Add(curLine.PadRight(maxWidth));
                            curLine = "";
                        }

                        if (word.Length <= maxWidth)
                        {
//if length of a word is <= to specified width
                            if (string.IsNullOrEmpty(curLine))
                                curLine = word;
                            else
                                curLine += " " + word;
                        }
                        else
                        {
//force a word to split if it is > to specified width
                            if (!string.IsNullOrEmpty(curLine))
                            {
                                lstLines.Add(curLine.PadRight(maxWidth));
                                curLine = "";
                            }
                            for (int j = 0; j < word.Length; j += maxWidth)
                            {
                                if (j + maxWidth < word.Length)
                                    lstLines.Add(word.Substring(j, maxWidth));
                                else
                                    lstLines.Add(word.Substring(j).PadRight(maxWidth));

                            }
                        }

                    }
                    if (!string.IsNullOrEmpty(curLine))
                    {
                        lstLines.Add(curLine.PadRight(maxWidth));
                    }
                }
            }


            return lstLines;
       }


</code>


Any modification or suggestion on this is highly appreciated.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalfor R Verma Pin
Ravi LVS1-Mar-10 15:44
Ravi LVS1-Mar-10 15:44 
GeneralRe: for R Verma Pin
Chetan Patel4-Mar-10 22:38
Chetan Patel4-Mar-10 22:38 
GeneralRe: for R Verma Pin
Ravi LVS4-Mar-10 23:48
Ravi LVS4-Mar-10 23:48 
GeneralRe: for R Verma [modified] Pin
r verma18-Mar-10 23:25
r verma18-Mar-10 23:25 
GeneralMy approach... Pin
supercat91-Mar-10 5:14
supercat91-Mar-10 5:14 
GeneralRe: My approach... Pin
Ravi LVS1-Mar-10 15:48
Ravi LVS1-Mar-10 15:48 
GeneralRe: My approach... Pin
supercat92-Mar-10 6:11
supercat92-Mar-10 6:11 
GeneralRe: My approach... Pin
Ravi LVS2-Mar-10 15:34
Ravi LVS2-Mar-10 15:34 
GeneralRe: My approach... Pin
supercat93-Mar-10 7:26
supercat93-Mar-10 7:26 
GeneralGo one Pin
khamis1-Mar-10 3:36
khamis1-Mar-10 3:36 
Hi,

thanks for the idea, i will convert Shucks | :-\ this code to javascript Sigh | :sigh: to keep grids design (columns fixed width).

al-moghrabi.
GeneralRe: Go one Pin
Ravi LVS1-Mar-10 15:49
Ravi LVS1-Mar-10 15:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.