c# - 向右移动整行字符串

标签 c# string

        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);
        }

读取一个文本文件,它为每一行在开头添加一个空格,删除所有双倍和以上的空格,我想将行向右移动,但它什么也没做。

结果: Result of the program

预期结果:enter image description here

最佳答案

如果已经达到指定的长度,

PadLeftPadRight 不会在字符串的开头/结尾添加字符。

来自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/

相关文章:

c# - 如果使用 Entity Framework 与列表中的任何匹配

c# - 如何将对象转换为泛型?

c++读取txt到字符串错误

r - 提高在大字符串向量上计算词分数总和的性能?

C# 在 select 语句中引用 Excel 工作表索引

c# - .Net Core 3.1 使用 MySql 向现有项目添加身份

c# - 使用ListBox存储多条数据

java - 如何检查字符串是否等于有效字符之一? (棋盘)

linux - 在 BASH 中处理字符串并计算看到的子字符串的数量

c# - 将 NULL 日期时间值从 T-SQL 返回到 C# DataTable ==> DataRow 而不是空字符串