使用maxchar对字符串进行居中和分割
本文关键字:分割 maxchar 字符串 使用 | 更新日期: 2023-09-27 18:11:04
我有很多时间思考如何解决,这是我的功能:
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;
}
这个函数将一个字符串分成36个字符的部分,然后用空格添加结果字符,使文本居中:例如:
string x = "27 Calle, Col. Ciudad Nueva, San Pedro Sula, Cortes";
TextAlignCenter(x);
结果:Line1: 27 Calle, Col. Ciudad Nueva, San Ped
Line2: ro Sula, Cortes
Line3:[EmptyLine]
Line1和Line2是正确的,但我不需要Line3,我怎么能防止打印这不必要的行?
更新:
将行添加到ArrayList()
ArrayList Lines = new ArrayList();
string x = "27 Calle, Col. Ciudad Nueva, San Pedro Sula, Cortes";
Lines.Add(TextAlignCenter(x));
Console.WriteLine(Lines.Count);
您只需要为完整块使用循环,并为最后一个设置一个特殊条件:
public static IEnumerable<String> SplitByLength(String value,
int size, // you may want to set default for size, i.e. "size = 36"
Char padding = ' ') {
if (String.IsNullOrEmpty(value))
yield break; // or throw an exception or return new String(padding, size);
if (size <= 0)
throw new ArgumentOutOfRangeException("size", "size must be a positive value");
// full chunks with "size" length
for (int i = 0; i < value.Length / size; ++i)
yield return value.Substring(i * size, size);
// the last chunk (if it exists) should be padded
if (value.Length % size > 0) {
String chunk = value.Substring(value.Length / size * size);
yield return new String(padding, (size - chunk.Length) / 2) +
chunk +
new String(padding, (size - chunk.Length + 1) / 2);
}
}
...
String source = "27 Calle, Col. Ciudad Nueva, San Pedro Sula, Cortes";
// I've set padding to '*' in order to show it
// 27 Calle,
// Col. Ciuda
// d Nueva, S
// an Pedro S
// ula, Corte
// ****s*****
Console.Write(String.Join(Environment.NewLine,
SplitByLength(source, 10, '*')));
你的样品:
string x = "27 Calle, Col. Ciudad Nueva, San Pedro Sula, Cortes";
List<String> lines = SplitByLength(x, 36).ToList();
这里不需要递归函数。还有一些操作不是最优的。
private static String TextAlignCenter(String Line)
{
int len = 36; // avoid magic numbers
StringBuilder b = new StringBuilder();
String CenterLine = String.Empty;
for (int i = 0; i < Line.Length; i += len)
{
if ((i + len) < Line.Length)
b.AppendLine(Line.Substring(i, len)); // add new line at the end
else
{
CenterLine = Line.Substring(i);
int CountLineSpaces = (len - CenterLine.Length) / 2;
// new string(' ', CountLineSpaces) replicates ' ' CountLineSpaces times
CenterLine = new string(' ', CountLineSpaces) + CenterLine;
b.Append(CenterLine); // string tail, no new line
}
}
Console.WriteLine(b.ToString());
return b.ToString();
}
小提琴演示
我会选择Console.Write
:
private void 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 = (36 - Line.Length) / 2;
for (int i = 0; i < CountLineSpaces; i++)
{
CenterLine += (Char)32;
}
CenterLine += Line + Environment.NewLine;
}
Console.Write(CenterLine);
}