How to wrap line using C#

A very common problem that we encounter when writing console application or windows application LINE WRAP" well those who are not familiar with the word. 

Then let me explain it for them line wrap means In text display, it is the feature of continuing on a new line when a line is full, such that each line fits in the viewable window.

I have written a sample code that will print only 10 letters in each line. You can set your own size limit by changing the value of variable "size".

using
System;
using
System.Collections.Generic;

using System.Linq;
using
System.Text;
using
System.Messaging;
using
System.Data;
using
System.Threading;
namespace
ConsoleApplication2
{
    class
Program

        static void Main(string[] args)
        {
            string s = "Programmer's Fav test string is Hello World";

            int size = 10;


            int i = Convert.ToInt32(Math.Ceiling(s.Length / size + .5));

 
            if (s.Length > size)
            {
                for (int j = 1; j <= i; j++)
                {
                    if (((j - 1) * size + size) < s.Length)
                    {
                        final = s.Substring((j * size) - size, size);
                    }
                   
else
                    {
                        final = s.Substring((j * size) - size, s.Length - ((j * size) - size));
                    }
                    sbd.Append(final + "\n");

                Console.WriteLine(sbd.ToString());
            }
           
else
            {
                Console.WriteLine(s);
            }
            Console.Read();
        }
    }
}

Ebook Download
View all
Learn
View all