简体   繁体   中英

Insert a newline and tab every nth character in string C#

I'm trying to use Regex.Replace in order to insert the \n\t characters at every nth position in the string. The problem is I don't want it inserted in the middle of a word.

What I have now:

Regex.Replace(inputString, "(.{85})", "$&\n\t")

I would also like to only insert a tab if there is already a newline present in the group of 85 characters (inserting the tab directly after the already present newline).

I assume you want to add a tab only if the 85th character is a newline, or if the 85th symbol is not a newline, add both a newline and a tab.

Then, you can use @"(?s).{0,85}" regex that will match any symbols from 0 to 85, as many as possible (it is a greedy quantifier) and check if the last character is a newline or not. Then, do what is necessary:

var str = "This is a really long test string is this is this is this is this is this is this is thisit is this this has to be way more than 85 characters ssssssssssss";
Console.WriteLine(Regex.Replace(str, @"(?s).{0,85}", 
        m => m.Value.EndsWith("\n") ? m.Value + "\t" : m.Value + "\n\t"));

Result of the demo :

This is a really long test string is this is this is this is this is this is this is 
    thisit is this this has to be way more than 85 characters ssssssssssss

If you need to only add a tab if there the 85-character match contains a newline, replace the .EndsWith("\\n") with .Contains("\\n") in the above code.

To avoid splitting in the middle of a word, add a word boundary: @"(?s).{0,85}\\b" . Or, if it is not always a word character at the end, use @"(?s).{0,85}(?!\\w)" . Another possible scenario is when you want to ensure at least 85 characters (or a bit more if the word boundary is not found), use @"(?s).{85,}?(?!\\w)" .

With some chat help from @Wiktor Stribiżew, here is an alternative:

  • preserves pre-existing newlines
  • if a line is longer than N chars, it breaks the line at whitespace and indents the next line(s) until the long line is consumed.
  • configurable max line length
  • configurable how/whether the broken lines whould be indented.
public static string AddLineBreaks(this string text, int maxLineLength, string indent = "\t")
{        
    // Strip off any whitespace (including \r) before each pre-existing end of line character.
    //
    text = Regex.Replace(text, @"\s+\n", "\n", RegexOptions.Multiline);

    // Matches that are too long include a trailing whitespace character (excluding newline)
    // which is then used to sense that an indent should occur
    // Regex to match whitespace except newline: https://stackoverflow.com/a/3469155/538763
    //
    string regex = @"(\n)|([^\n]{0," + maxLineLength + @"}(?!\S)[^\S\n]?)";

    return Regex.Replace(text, regex, m => m.Value +
        (m.Value.Length > 1 && Char.IsWhiteSpace(m.Value[m.Value.Length - 1]) ? ("\n" + indent) : ""));
}

在此处输入图片说明

Try backtick instead of \

Regex.Replace(inputString, "(.{85})", "$&`n`t")

Sometimes $ also needs backtick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM