Remove new line character from string c #

I have the following line.

    string str = @"One
Two

Four
Five
Six
Seven
Eight




Thirteen






Twenty


";

      

I want to remove extra lines on this line. To make the line look like this:

str = "One
Two
Four
Five
Six
Seven
Eight
Thirteen
Twenty"

      

I am using this code but it doesn't work.

 Str = Str.Replace("\n\n", "\n");
           while (Str.IndexOf("\n") > 0)
            {
                Str = Str.Replace("\n\n", "\n");
            }

      

I even tried with Str = Str.Replace("\u000a\u000a", "\u000a");

But still it didn't work.

+3


source to share


3 answers


You can split the line into lines, remove empty entries, and concatenate them together:



var lines = str.Split('\n')
                .Where(s => !string.IsNullOrWhiteSpace(s));

str = string.Join("\n", lines);

      

+7


source


Try the following:

str = System.Text.RegularExpressions.Regex.Replace(str, "(" + Environment.NewLine + ")+", Environment.NewLine)

      

For more details Environment.Newline

see here . But even the above code does not guarantee that duplicate lines will be removed, since the document or line you are processing might be created on another machine where the code for the newline is different:

  • "\r\n"

    - Windows newline,
  • "\n"

    - unix newline,
  • "\r

    "- mac newline


For an introduction to wikipedia regex, the article should be pretty informative, but overall:

  • Environment.Newline

    there can be multiple characters like "\r\n"

    , and so I include this variable in "()"

    to mark it as a group of characters (single element) to be considered atomic,
  • "+"

    matches the previous element ( Environment.Newline

    enclosed in "()"

    ) one or more times .

Thanks to the above, Regex.Replace

we get exactly the desired result.

+3


source


I tried your code and it hangs. What to expect since the replacement will never get rid of all the instances \n

. You want to change the current while loop like this:

while (str.IndexOf("\n\n") > 0)
{
    str = str.Replace("\n\n", "\n");
}

      

This will continue until all duplicate instances are removed \n\n

.

Edit: I've tested this for variety or cases as well, and it works as long as the line doesn't start with \n

or \n\n

.

0


source







All Articles