none
How to find the number lines in the notepad?

    Question

  • Hi,

    How to find the number lines in the notepad?Any one tell me the code plz...

    Regards,
    venkat

    venakt
    Friday, October 17, 2008 1:56 PM

Answers

  • As in the above post, read all lines from the file Like:-

    string[] lines;
    lines = System.IO.File.ReadAllLines(@"c:\a.txt");
    // This will return a string array...

    // Store the length of the array in a variable (ie number of lines.)
    int lineCount = lines.Length;

    // Search for blank lines...and subtract it from total no of lines.
    for each string str in lines
    {
       if(str.Trim().equals("")) lineCount--;
    }

    // Now lineCount will have the number of lines with data!!!
     Pls check with the syntax....Thank you...

    Aldo John, Nexgen Technology Services, Bangalore
    • Marked as answer by Zhi-Xin Ye Thursday, October 23, 2008 9:35 AM
    Monday, October 20, 2008 4:54 AM
  • If you're using Linq, the following statement would work for you:

    int numberOfLines = File.ReadAllLines(@"C:\file.txt").Count(s => s.Trim().Length > 0);


    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Blackwood Monday, October 20, 2008 7:02 PM
    • Marked as answer by Zhi-Xin Ye Thursday, October 23, 2008 9:35 AM
    Monday, October 20, 2008 10:25 AM

All replies

  •  

    View/StatusBar ?

    You can't have Word Wrap at the same time.

    This looks off-topic, it's not a programming question.


    Puzzles, brain teases, riddles, enigmas: http://www.toysforthebrain.com
    Friday, October 17, 2008 2:12 PM
  • int numberOfLines = File.ReadAllLines(@"C:\myfile.txt").Length + 1;
    David Morton - http://blog.davemorton.net/
    Friday, October 17, 2008 4:35 PM
  • Hi ,

    I want the number of lines which contains the data.that is i want to ignore the empty rows and i want the number of lines which contains the data.Any one give me the code plz...


    Regards,
    venkat.

    venakt
    Monday, October 20, 2008 4:37 AM
  • As in the above post, read all lines from the file Like:-

    string[] lines;
    lines = System.IO.File.ReadAllLines(@"c:\a.txt");
    // This will return a string array...

    // Store the length of the array in a variable (ie number of lines.)
    int lineCount = lines.Length;

    // Search for blank lines...and subtract it from total no of lines.
    for each string str in lines
    {
       if(str.Trim().equals("")) lineCount--;
    }

    // Now lineCount will have the number of lines with data!!!
     Pls check with the syntax....Thank you...

    Aldo John, Nexgen Technology Services, Bangalore
    • Marked as answer by Zhi-Xin Ye Thursday, October 23, 2008 9:35 AM
    Monday, October 20, 2008 4:54 AM
  • If you're using Linq, the following statement would work for you:

    int numberOfLines = File.ReadAllLines(@"C:\file.txt").Count(s => s.Trim().Length > 0);


    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Blackwood Monday, October 20, 2008 7:02 PM
    • Marked as answer by Zhi-Xin Ye Thursday, October 23, 2008 9:35 AM
    Monday, October 20, 2008 10:25 AM