const string Duom = "Text.txt";
char[] seperators = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' };
string[] lines = File.ReadAllLines(Duom, Encoding.GetEncoding(1257));
for (int i = 0; i < lines.Length; i++)
{
string GLine = " " + lines[i];
GLine = Regex.Replace(GLine, @"\s+", " ");
GLine = GLine.PadRight(5, ' ');
Console.WriteLine(GLine);
}
读取一个文本文件,它为每一行在开头添加一个空格,删除所有双倍和以上的空格,我想将行向右移动,但它什么也没做。
最佳答案
如果已经达到指定的长度,
PadLeft
和 PadRight
不会在字符串的开头/结尾添加字符。
来自docs for String.PadRight (强调我的):
Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.
您的所有字符串都大于 5
,即指定的总长度,因此 PadRight/PadLeft 不会执行任何操作。
“填充”字符串是添加空格(或其他一些字符),以便新字符串至少与您想要的数字一样大。
相反,只需在字符串前手动添加 5 个空格。
GLine = " " + GLine;
或者更编程:
GLine = new string(' ', 5) + GLine;
关于c# - 向右移动整行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53505776/