1.1K
In this example we show you a way to count the amount of lines in a string
Example
We use a while loop to count the lines in string.
We check the index of sentence is equal to -1. If the condition is true, then execute the statement and print the number of lines in the string.
using System; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static void Main() { long count = lineCounter("This is \n a \n text \n string as an \n example"); Console.WriteLine("Number of Lines in the String : {0}", count); Console.ReadLine(); } static long lineCounter(string myString) { long count = 1; int start = 0; while ((start = myString.IndexOf('\n', start)) != -1) { count++; start++; } return count; } } }
Lets see a test run
Number of Lines in the String : 5