简体   繁体   中英

NewLine replacement c#?

I have a windows phone application. In the application I have a textbox whose acceptsreturn property is set to true.

What I want to do is create a string from the textbox and replace the new lines with a specific character, something like "NL"

I've tried the following but none of them worked.

string myString = myTextBox.Text.Replace(Environment.NewLine,"NL");
string myString = myTextBox.Text.Replace("\n","NL");

I'm not familiar with windows phone(or silverlight), but try to split with \\r instead:

string myString = myTextBox.Text.Replace("\r","NL");

Why does a Silverlight TextBox use \\r for a newline instead of Environment.Newline (\\r\\n)?

考虑更换不同类型的换行符以处理所有可能性

string myString myTextBox.Replace("\r\n", "NL").Replace("\n", "NL").Replace("\r", "NL");

Use this code

var myString = myTextBox.Text.Replace("\r","NL");

This is for compatibility with every operating systems.

A question with a similar topic had a quite elegant answer you might consider interesting to use:

using System.Text.RegularExpressions;

myTextBox.Text = Regex.Replace(myTextBox.Text, @"\r(?!\n)|(?<!\r)\n", "NL");

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