代码之家  ›  专栏  ›  技术社区  ›  Samantha

c#制表符换行字符串

  •  0
  • Samantha  · 技术社区  · 12 年前

    我正在给一个文本文件写一个大字符串(大约100行),希望整个文本块都用选项卡显示。

    WriteToOutput("\t" + strErrorOutput);
    

    我在上面使用的行只标记文本的第一行。如何缩进/制表整个字符串?

    5 回复  |  直到 12 年前
        1
  •  1
  •   nvoigt    12 年前

    用换行符和制表符替换所有换行符:

    WriteToOutput("\t" + strErrorOutput.Replace("\n", "\n\t"));
    
        2
  •  1
  •   Anirudha    12 年前
    File.WriteAllLines(FILEPATH,input.Split(new string[] {"\n","\r"}, StringSplitOptions.None)
                                     .Select(x=>"\t"+x));
    
        3
  •  1
  •   Will Custode    12 年前

    要做到这一点,你必须有一个有限的行长度(即<100个字符),这时这个问题就变得很容易了。

    public string ConvertToBlock(string text, int lineLength)
    {
        string output = "\t";
    
        int currentLineLength = 0;
        for (int index = 0; index < text.Length; index++)
        {
            if (currentLineLength < lineLength)
            {
                output += text[index];
                currentLineLength++;
            }
            else
            {
                if (index != text.Length - 1)
                {
                    if (text[index + 1] != ' ')
                    {
                        int reverse = 0;
                        while (text[index - reverse] != ' ')
                        {
                            output.Remove(index - reverse - 1, 1);
                            reverse++;
                        }
                        index -= reverse;
                        output += "\n\t";
                        currentLineLength = 0;
                    }
                }
            }
        }
        return output;
     }
    

    这将把任何文本转换为一个文本块,该文本块被分解为长度行 lineLength 所有这些都以一个制表符开始,以一个换行符结束。

        4
  •  0
  •   jltrem    12 年前

    您可以制作一个字符串的副本用于输出,将CRLF替换为CRLF+TAB。然后将该字符串写入输出(仍以初始TAB为前缀)。

    strErrorOutput = strErrorOutput.Replace("\r\n", "\r\n\t");
    WriteToOutput("\t" + strErrorOutput);
    
        5
  •  0
  •   Nimai    3 年前

    如果你在这里寻找一种将字符串换行到一定宽度,并使每一行缩进的方法(就像我以前一样),这里有一个作为扩展方法的解决方案。大致基于上面的答案,但使用正则表达式将原始文本拆分为单词和空格对,然后重新加入它们,根据需要添加换行符和缩进。(不进行健全性检查输入,因此如果需要,您需要添加它)

    public string ToBlock(this string text, int lineLength, string indent="")
    {
        var r = new Regex("([^ ]+)?([ ]+)?");
        var matches = r.Match(text);
        if (!matches.Success)
        {
            return text;
        }
        string output = indent;
        int currentLineLength = indent.Length;
        while (matches.Success)
        {
            var groups = matches.Groups;
            var nextLength = groups[0].Length;
            if (currentLineLength + nextLength <= lineLength)
            {
                output += groups[0];
                currentLineLength += groups[0].Length;
            }
            else
            {
                if (currentLineLength + groups[1].Length > lineLength)
                {
                    output += "\n" + indent + groups[0];
                    currentLineLength = indent.Length + groups[0].Length;
                }
                else
                {
                    output += groups[1] + "\n" + indent;
                    currentLineLength = indent.Length;
                }
            }
            matches = matches.NextMatch();
        }
        return output;
    }