How can I get only the first line from a string?

.NET already has a line-reader: StringReader. This saves worrying about what constitutes a line break, and whether there are any line breaks in the string.

using (var reader = new StringReader(str))
{
    string first = reader.ReadLine();
}

The using statement is used for disposing any non-managed resources consumed by the reader object. When using C#8 it can be simplified like so:

using var reader = new StringReader(str);
string first = reader.ReadLine(); 

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)