public static List
{
List
List
string[] commaSeperatedStringsArray = null;
List
//split strings into list of comma seperated array of strings and calculate maximum length of string array.
foreach (string str in strings)
{
commaSeperatedStringsArray = str.Split(seperator);
//remove all trailing and leading spaces.
for (int index = 0; index < commaSeperatedStringsArray.Length; ++index)
{
commaSeperatedStringsArray[index] = commaSeperatedStringsArray[index].Trim();
//initialize maximum characters in each column.
if (maxColumnWidthArray.Count > index)
{
maxColumnWidthArray[index] = (maxColumnWidthArray[index] < commaSeperatedStringsArray[index].Length) ? commaSeperatedStringsArray[index].Length : maxColumnWidthArray[index];
}
else
{
maxColumnWidthArray.Add(commaSeperatedStringsArray[index].Length);
}
}
ListOfCommaSeperatedStringsInLine.Add(commaSeperatedStringsArray);
}
StringBuilder formattedString = new StringBuilder();
int accumlativeMaxLength = 0;
int pad = 0;
//Insert spaces to align text and append in single line text.
foreach (string[] stringsInLine in ListOfCommaSeperatedStringsInLine)
{
formattedString.Append(stringsInLine[0]);
accumlativeMaxLength = maxColumnWidthArray[0];
for (int index = 1; index < maxColumnWidthArray.Count; ++index)
{
if (stringsInLine.Length > index)
{
pad = accumlativeMaxLength - formattedString.Length;
formattedString.Append(seperator);
formattedString.Append(Constants.SYMBOL_SPACE_CHARACTER, pad);
if (IsNumeric(stringsInLine[index]))
{
formattedString.Append(Constants.SYMBOL_SPACE_CHARACTER, maxColumnWidthArray[index] - stringsInLine[index].Length);
}
formattedString.Append(stringsInLine[index]);
accumlativeMaxLength += maxColumnWidthArray[index] + 1;
}
}
formattedStringList.Add(formattedString.ToString());
formattedString.Length = 0;
}
return formattedStringList;
}
This is very useful method when you have to align certain strings and there is a seperator then this method will allign all the lines, just pass the array of strings and a seperator.
0 comments:
Post a Comment