ASP.NET - How to divide long string into two or three line

Asked By bhanupratap singh on 17-Sep-13 07:56 AM
how to divide long string into two three line.  Actually I want to divide a column value which is coming from database in to dataset
column name is "ADDRESS". It contains more than 100 charecters . I need to divide the value into two line and show the value in a seprate label. 

for exam:- My column value is " 110, New Modern Shahdra , Gali No 20, Mandoli Road Near Nathu Colony chowck. 

now I want to Show thease value in two seprate label.

like 
lblTextAdd1.Text=110, New Modern Shahdra , Gali No 20,;
lblTextAdd2.Text=Mandoli Road Near Nathu Colony chowck. ;

pls reply
thanks

Robbe Morris replied to bhanupratap singh on 17-Sep-13 08:47 AM
string address = "This is my address and some stuff about 110, NewModern Shahra, Gali No 20";

var pieces = address.Split(" ".ToCharArray());

Also look at the .Substring method on a string.  Most people break the address up into words or pieces.  Then, build new strings by iterating through the pieces until they reach a max length that includes a whole word (sometimes the next word could take you from 95 characters to 120) enabling them to skip to the next line if the next word is too long.
bhanupratap singh replied to Robbe Morris on 17-Sep-13 09:12 AM
thanks for reply
it's working.
one thing more. how to count pieces . it showing there are five items or more. I have to set value of each splited words on different leble.text how to do thanks
Robbe Morris replied to bhanupratap singh on 17-Sep-13 10:45 AM
Arrays have a .Length property to tell you how many elements are in the array.
Vinitha Jayakrishnan replied to bhanupratap singh on 18-Sep-13 07:07 AM
You can go with this method::::
static void Main(string[] args)
{
    const Int32 MAX_WIDTH = 60;
    int offset = 0;
    string text = Regex.Replace(File.ReadAllText("oneline.txt"), @"\s{2,}", " ");
    List<string> lines = new List<string>();
    while (offset < text.Length)
    {
        int index = text.LastIndexOf(" ", 
                         Math.Min(text.Length, offset + MAX_WIDTH));
        string line = text.Substring(offset,
            (index - offset <= 0 ? text.Length : index) - offset );
        offset += line.Length + 1;
        lines.Add(line);
    }
}