简体   繁体   中英

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.

var result = s.Replace(Environment.NewLine, Environment.NewLine + Environment.NewLine);
string newString = oldString.Replace("\n", "\n\n");
string newString = oldString.Replace(Environment.NewLine, Environment.NewLine + Environment.NewLine);

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

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

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:)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM