如何在 C# 控制台中的整个段落的左侧和右侧添加填充?
How to add padding to the left and right of a whole paragraph in C# Console?
我在弄清楚如何为整个文本正文添加填充时遇到了问题。我的代码如下。
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;
}
我得到的:
需要输出(!):
您必须更改程序的 Console.Write 部分
正如本期中的精彩解释:
你必须声明最大行宽,并且对于你在控制台上写的每一行,你应该写一个长度为 leftMargin
的空字符串
每一行都必须单独解析。
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;
}
我在弄清楚如何为整个文本正文添加填充时遇到了问题。我的代码如下。
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;
}
我得到的:
需要输出(!):
您必须更改程序的 Console.Write 部分
正如本期中的精彩解释:
你必须声明最大行宽,并且对于你在控制台上写的每一行,你应该写一个长度为 leftMargin
每一行都必须单独解析。
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;
}