Linebreak in WinForms text box

I have problems with spreads here. In principle, the user places some data in a text field, which is then stored directly in the database.

When I show the data in my program, I only want to display the first line of the text field, so I tried

Regex newLine = new Regex("/[\r\n]+/");
String[] lines = newLine.Split(row[data.textColumn.ColumnName].ToString());
SomeLabel.Text = lines[0];

But it displays me all the lines after another, so if the user puts

a
b
c

The label displays

abc

How can I make this work so that it only displays the first line?

+3
source share
2 answers
var data = row[data.textColumn.ColumnName].ToString();

( unix windows line-seperators). - , , .

int min = Math.Min(data.IndexOf("\r\n"), data.IndexOf("\n"));

string line;

if (min != -1)
    line = data.Substring(0, min);
else
    line = data;

var lines = data.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
var line = lines[0];

(. , : ?)

+1

( , , , - , , )

, :

public static IEnumerable<string> Lines(this string data)
{
    using (var sr = new StringReader(data))
    {
        string line;

        while ((line = sr.ReadLine()) != null)
            yield return line;
    }
}

:

var line = data.Lines().First();

, .Split, .

+2

Source: https://habr.com/ru/post/1760725/


All Articles