How to count the number of lines from a string in C#

1 Answer

0 votes
using System;

class Program
{
    public static int countLines(String s) {
         return s.Split("\n").Length;
    }
    static void Main() {
        String str = "line1\nline2\nline3\nline4\nlin5\nline6";      
        int count = countLines(str);

        Console.Write(count);
    }
}




/*
run:

6

*/

 



answered Aug 19, 2021 by avibootz

Related questions

1 answer 211 views
1 answer 116 views
116 views asked Mar 11, 2017 by avibootz
1 answer 170 views
170 views asked Mar 11, 2017 by avibootz
1 answer 149 views
1 answer 180 views
1 answer 191 views
2 answers 198 views
...