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 splits a string of 36 characters, then adds the resulting characters with spaces to create centralized 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 do not need line 3, how can I prevent this unnecessary line from printing?
UPDATE:
Lines are added to ArrayList ()
ArrayList Lines = new ArrayList(); string x = "27 Calle, Col. Ciudad Nueva, San Pedro Sula, Cortes"; Lines.Add(TextAlignCenter(x)); Console.WriteLine(Lines.Count);
source share