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 161 views
161 views asked Jul 26, 2018 by avibootz
1 answer 126 views
1 answer 120 views
1 answer 122 views
1 answer 110 views
1 answer 113 views
1 answer 127 views
...