I have many hours thinking how i can solve, this is my funtion:
private String TextAlignCenter(String Line)
{
String CenterLine = String.Empty;
if (Line.Length > 36)
{
for (int i = 0; i < Line.Length; i += 36)
{
if ((i + 36) < Line.Length)
TextAlignCenter(Line.Substring(i, 36));
else
TextAlignCenter(Line.Substring(i));
}
}
else
{
Int32 CountLineSpaces = (int)(36 - Line.Length) / 2;
for (int i = 0; i < CountLineSpaces; i++)
{
CenterLine += (Char)32;
}
CenterLine += Line;
}
Console.WriteLine(CenterLine);
return CenterLine;
}
This function split a string in parts of 36 characters then add the resultant characters with spaces for make a centered text: For example:
string x = "27 Calle, Col. Ciudad Nueva, San Pedro Sula, Cortes";
TextAlignCenter(x);
Result:
Line1: 27 Calle, Col. Ciudad Nueva, San Ped
Line2: ro Sula, Cortes
Line3:[EmptyLine]
Line1 and Line2 are correct but i dont need the Line3, how i can prevent to print this unnecessary line?
UPDATE:
The lines are added into a ArrayList()
ArrayList Lines = new ArrayList();
string x = "27 Calle, Col. Ciudad Nueva, San Pedro Sula, Cortes";
Lines.Add(TextAlignCenter(x));
Console.WriteLine(Lines.Count);