开发者

Get first line in textbox c#

c# I need to get the first line on a multi line text开发者_StackOverflow社区.box

every time I try this I get the whole textbox

Thanks for the help


Use the TextBox.Lines property which is a string[] array representing the lines of text:

textBox.Lines.Count > 0 ? textBox.Lines[0] : null

or alternatively (and shorter):

textBox.Lines.FirstOrDefault()


myTextBox.Text.Split(Environment.NewLine).FirstOrDefault();


yourtextbox.Lines[0]


if you split the text by \n character you will get the text as line by line as a string array. The first index of the array is the first row of your multine textbox. for example:

string firstLine = TextBox1.Text.Split('\n')[0];

This is for textbox in asp.net controls. If you want to obtain the same thing in a windows form application, you should write example code above:

string firstLine = textBox1.Lines[0];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜