How to get the first line of a multi-line string in C#

1 Answer

0 votes
using System;

public class GetTheFirstLineOfAMultiineString_CSharp
{
	public static void Main(string[] args)
	{
		string multiLineString = 
		        "First line\n" + 
		        "Second line\n" + 
		        "Third line\n" + 
		        "Fourth line";

		string firstLine = multiLineString.Substring(0, multiLineString.IndexOf("\n", StringComparison.Ordinal));

		Console.WriteLine("The first line is: " + firstLine);
	}
}



/*
run:
    
The first line is: First line
    
*/

 



answered Aug 13, 2024 by avibootz

Related questions

1 answer 166 views
166 views asked Jul 26, 2018 by avibootz
1 answer 132 views
1 answer 124 views
1 answer 131 views
1 answer 115 views
1 answer 118 views
1 answer 134 views
...