c# - 如何在 C# 控制台中为整个段落的左右添加填充?
问题描述
我在弄清楚如何为整个文本添加填充时遇到问题。我的代码如下。
public static String Format(List<string> list, int leftMargin, int rightMargin)
{
// Create an empty string to put the list of words into
string listTurnedIntoAString = "";
// Define the type of punctuation that is acceptable for have a space after.
string punctuation = ".,?!";
// turn the list of strings into one string
foreach (string item in list)
{
// if we find a punctuation mark remove the space before it
if (item.IndexOfAny(punctuation.ToCharArray()) == 0 && listTurnedIntoAString != String.Empty)
{
// Remove the space before the punctuation mark
listTurnedIntoAString = listTurnedIntoAString.Remove(listTurnedIntoAString.Length - 1);
}
// Add the next word to the string.
listTurnedIntoAString += $"{item} ";
}
// Add padding the left side
var stringPaddedLeft = listTurnedIntoAString.PadLeft(leftMargin + listTurnedIntoAString.Length);
// Add padding to the right side
var stringPaddedBothSides = stringPaddedLeft.PadRight(rightMargin + stringPaddedLeft.Length);
// Return the formated string
return stringPaddedBothSides;
}
解决方案
每一行都必须单独解析。
public static String Format(List<string> list, int leftMargin, int rightMargin)
{
// Create an empty string to put the list of words into
string listTurnedIntoAString = "";
// Define the type of punctuation that is acceptable for have a space after.
string punctuation = ".,?!-;";
// turn the list of strings into one string
foreach (string item in list)
{
// if we find a punctuation mark remove the space before it
if (item.IndexOfAny(punctuation.ToCharArray()) == 0 && listTurnedIntoAString != String.Empty)
{
// Remove the space before the punctuation mark
listTurnedIntoAString = listTurnedIntoAString.Remove(listTurnedIntoAString.Length - 1);
}
// Add the next word to the string.
listTurnedIntoAString += $"{item} ";
}
// Number of charters between each margin
int betweenMarginSpacing = rightMargin - leftMargin;
// Placeholder for the a new formatted string
string formattedString = "";
while (listTurnedIntoAString != String.Empty)
{
// check if we are close to the end of the list
if (listTurnedIntoAString.Length > betweenMarginSpacing)
{
// format each line
formattedString += $"{listTurnedIntoAString.Substring(0, betweenMarginSpacing).PadLeft(leftMargin + betweenMarginSpacing)}\n";
// remove the text from the unformatted string after it has been added to the formatted string.
listTurnedIntoAString = listTurnedIntoAString.Substring(betweenMarginSpacing);
}
else // we are at the end of the string
{
// append the rest of the string to the formatted string.
formattedString += listTurnedIntoAString.PadLeft(leftMargin + listTurnedIntoAString.Length);
// declare the formatted string empty
listTurnedIntoAString = String.Empty;
}
}
// Return the formated string
return formattedString;
}
推荐阅读
- c++ - CMake 与 Eigen 的链接错误“CMake 无法确定目标的链接器语言”
- python - 如何使用 CNN 预测孔的中心坐标
- c# - 如何为 NI 示例等 VBA 创建静态函数 TLB 文件?
- javascript - 无法使用 Selenium 和 java/javascript 在字段中输入数据
- nativescript - 如何在使用 nativescript-Angular 键入和更改 Android 设备的光标指针颜色时删除输入文本中的下划线
- oracle - 我想调用这个过程并传递在游标中获取的变量
- node.js - 复制“Concourse CI”节点调用/调试 process.stdin Net.socket
- c# - DbContext.AttachRange() 在这种情况下如何工作
- python - 通过 Anaconda 安装 libm.so.6
- javascript - 如何通过nodejs(通过heroku)下载文件并将它们存储到s3?