taito taito - 4 years ago 105
C# Question

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

example:

string str = "Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.";

string firstline;


How can I get just the first line from the string "str" into the string "firstline"?

Answer Source

Assuming your string really contains new lines (\r and \n), you can use:

string line1 = str.Split(new [] { '\r', '\n' }).FirstOrDefault()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download