1

I would like to make a function that break lines from a string.

eg.: "1. " -> "\n1. "

so i could write a code like this

string Input = "1. First option";
Input += "2. Second option";
Input += "3. Third option";

Output = WriteMenu(Input);

and get a string like this

"1. First option
\n2. Second option
\n3. Third option"

The pattern will always be [number][dot][whitespace]. It's not a problem if the first option came with new line.

1
  • So just to be clear (because it seems the current answers have overlooked this): you don't want a CRLF in front of the 1.? Commented Apr 4, 2012 at 0:15

3 Answers 3

4

Give this guy a shot

Input = Regex.Replace(Input, @"(?<!^)(\d+\.)", "\n$1")
Sign up to request clarification or add additional context in comments.

Comments

2
Regex rgx = new Regex("(\\d+\\.\\s)");
String replaced = rgx.Replace(Input, Environment.NewLine + "$1");

Comments

1

A bit shorter expression like this would work too:

Regex.Replace(Input, @"(?!^)\d+\.", "\n$0")

Comments

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.