开发者

Split a string if it contains enter key or \n

I have a multiline textbox. I am entering recipe ingredients one by one in each line by using enter key. I am saving this to database in a si开发者_开发知识库ngle field, but on display I need to split them to show them line by line. How can I achieve this?


No special effort should be needed.

If the user enters a multi-line string and you save it to the database like that, when you fetch it from the database again, it will still be a multi-line string.


When you display, have such code:

strValueFromDb.Split('\n').ToList().ForEach(line =>
{
    lbDisplay.Text += line + "<hr />";
});

This example will show each line followed by <hr /> you can do whatever you want of course.


Can't your database handle '\n'? If not (?), then replace it with a 'placeholder' such as '::' and then swap it back to '\n' when displaying it.


You can use Split() method of a string:

string[] lines = "Line 1\r\nLine2\r\nLine 3".Split(new string[] { Environment.Newline }, StringSplitOptions.None);


You can use Environment.Newline

txtRecipe.Text = "First line" + Environment.Newline + "Second line";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜