Replace 1 line break with 2 line breaks in c#

How can I stretch some string element _text from

A
B
C

to

A

B

C

?

Actually, I have some text getting from DB

_text = this.NormalizeString(DinamicLibrary.LoadText(DinamicLibrary.Path[(int)_category] + _textdllName, this.TextNumber));

What should I do with this query or with _text later to get what I want? I understand that I should change \n to \n\n but dunno how.

Upvotes: 4

Views: 4922

Answers (7)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56222

var result = s.Replace(Environment.NewLine, Environment.NewLine + Environment.NewLine);

Upvotes: 12

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56222

To match from 3rd \r\n you can use regex: (?s)(?<=(?:\r\n[^\r\n]*){2})\r\n, e.g.:

var s = @"A
B
C
D
E
F";
var result = Regex.Replace(s, @"(?s)(?<=(?:\r\n[^\r\n]*){2})\r\n", Environment.NewLine + 
    Environment.NewLine);

Output:

A
B
C

D

E

F

Upvotes: 1

gekowa
gekowa

Reputation: 452

typically a line break is a combination of \r and \n, which equals to Environment.NewLine("\r\n"). But a single \n can also perform a line break. Both are good, just use replace method to add another line breaker.

Upvotes: 0

Zinnsoldat
Zinnsoldat

Reputation: 11

You can use Environment.NewLine and String.Replace()

_text.Replace("\n", Environment.NewLine + Environment.NewLine);

Upvotes: 0

dknaack
dknaack

Reputation: 60556

string newString = oldString.Replace(Environment.NewLine, Environment.NewLine + Environment.NewLine);

Upvotes: 1

C.Evenhuis
C.Evenhuis

Reputation: 26446

You can duplicate newlines, using _text.Replace("\n", "\n\n");. You actually provided that answer, I just knew the name of the method to call :)

Upvotes: 0

Petar Ivanov
Petar Ivanov

Reputation: 93090

string newString = oldString.Replace("\n", "\n\n");

Upvotes: 2

Related Questions