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 200 views
1 answer 113 views
113 views asked Mar 11, 2017 by avibootz
1 answer 164 views
164 views asked Mar 11, 2017 by avibootz
1 answer 142 views
1 answer 176 views
1 answer 185 views
2 answers 191 views
...