3

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 "
2
  • Can you try newString = newString.Replace("\r\n", "\r\n ").Trim(); ? Commented Apr 6, 2016 at 12:02
  • Sorry - added more detail as I realised I had omitted an important part of my question. Commented Apr 6, 2016 at 12:09

2 Answers 2

4

You can use a look ahead and look behind so that you only match the "between" part.

string test = "\r\n\r\n\r\n";
Regex reg = new Regex("(?<=\r\n)(?=\r\n)");
test = reg.Replace(test, " ");

That will result in test being "\r\n \r\n \r\n".

Sign up to request clarification or add additional context in comments.

1 Comment

Just plugged this baby into my unit test and it passed first time... thanks!
-1
"\r\n\r\n\r\n".Replace("\n\r", "\n \r");

3 Comments

Sorry - please see my edit on the qquestion - this is not what I am trying to do
This is over simplifying. A string like "\n\r" should not get a space inserted based on the OP's requirements.
Juharr, "\n\r" may use as variable. This is just example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.