Reputation: 4100
I have following string.
string str = @"One
Two
Four
Five
Six
Seven
Eight
Thirteen
Twenty
";
I want to remove the extra new lines in this string. So that the string should look like:
str = "One
Two
Four
Five
Six
Seven
Eight
Thirteen
Twenty"
I am using this code but it is not working.
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 worked out.
Upvotes: 3
Views: 4429
Reputation: 5705
Try this:
str = System.Text.RegularExpressions.Regex.Replace(str, "(" + Environment.NewLine + ")+", Environment.NewLine)
See here to learn more about Environment.Newline
. But even the above code does not guarantee to remove duplicate newlines, because the document or string you are parsing could be created on different machine where the code for a newline is diferent:
"\r\n"
- windows newline,"\n"
- unix newline,"\r
" - mac newlineFor introduction to regular expression, wikipedia article should be quite informative, but generally:
Environment.Newline
can be of multiple characters, such as "\r\n"
and thats why I am enclosing this variable in "()"
to mark it as a group of characters (single element) which should be considered atomic,"+"
matches the preceding element (Environment.Newline
enclosed in "()"
) one or more times.Thanks to above and to Regex.Replace
we get exactly the desired output.
Upvotes: 3
Reputation: 119156
You could split the string into lines, remove the empty entries and join it back together:
var lines = str.Split('\n')
.Where(s => !string.IsNullOrWhiteSpace(s));
str = string.Join("\n", lines);
Upvotes: 7
Reputation: 298
I tried your code and it hangs at the while. Which is to be expected as the replace will never get rid of all of the \n
instances. You want to change your current while loop to this:
while (str.IndexOf("\n\n") > 0)
{
str = str.Replace("\n\n", "\n");
}
This will loop until any repeated instances of \n\n
have been removed.
Edit: I've tested this and for a variety or cases and it works as long as the string does not start with \n
or \n\n
.
Upvotes: 0