I have a string. For simplicity it could look like this:
\r\n\r\n\r\n
It could also have more occurrences '\r\n' - I'm only posting the simplest problem here.
I need to insert a space between each occurrence of '\r\n\r\n' For the example above, I expect to end up with a string that looks like:
\r\n \r\n \r\n
I CAN do this using the following code (which I don't like because it creates a lot of strings in order to produce the expected result):
string newString = "\r\n\r\n\r\n";
while (newString.Contains("\r\n\r\n"))
newString = newString.Replace("\r\n\r\n", "\r\n \r\n");
I have had problems trying to use string.Replace or figure out a regular expression that can do this for me.
If I try to use string.replace (or even a StringBuilder.Replace) like so...
var result = "\r\n\r\n\r\n".Replace("\r\n\r\n", "\r\n \r\n");
I end up with
"\r\n \r\n\r\n" //There is no space between the last two carriage return / line feed
I think that a regex replace could do the trick, but I can't figure out how to get the regex engine to do a lazy match in order to replace all occurrences of \r\n after the first replace.
I have been searching both SO and google trying to find a similar problem, but haven't turned up anything of use. I apologise in advance if this is a duplicate, but if it is, I can't find it!
So in summary, I can do this using the while loop, but I wanted to know if there was a more elegant solution?
EDIT
Sorry - there is a bit more to this: My code may also review a string that looks like this:
5\r\n\r\nTest\r\n
In which case, I would expect to see a string that looks like this:
5\r\n \r\nTest\r\n
So - I don't want to just replace '\r\n' with '\r\n ' as this would give me a string that looks lke:
"5\r\n \r\n Test\t\n "