Recipe 10.9. Counting Lines of TextProblemYou need to count lines of text within a string or within a file. SolutionUse the LineCount method shown in Example 10-8 to read in the entire file and count the number of line feeds. Example 10-8. LineCount method
LineCount2, a better performing alternate version of this method uses the StreamReader.ReadLine method to count lines in a file and a regular expression to count lines in a string, as shown in Example 10-9. Example 10-9. LineCount2 method
The following method counts the lines within a specified text file and a specified string: public static void TestLineCount( ) { // Count the lines within the file TestFile.txt. LineCount(@"C:\TestFile.txt", true); // Count the lines within a string. // Notice that the \r\n characters start a new line // as well as just the \n character. LineCount("Line1\r\nLine2\r\nLine3\nLine4", false); } DiscussionEvery line ends with a special character. For Windows files, the line-terminating characters are a carriage return followed by a line-feed. This sequence of characters is described by the regular expression pattern \r\n. Unix files terminate their lines with just the line-feed character (\n). The regular expression "\n" is the lowest common denominator for both sets of line-terminating characters. Consequently, this method runs a regular expression that looks for the pattern "\n" in a string or file.
Simply running this regular expression against a string returns the number of lines minus one because the last line does not have a line-terminating character. To account for this, one is added to the final count of line feeds in the string. The LineCount method accepts two parameters. The first is a string that either contains the actual text that will have its lines counted or the path and name of a text file whose lines are to be counted. The second parameter, isFileName, determines whether the first parameter (source) is a string or a file path. If this parameter is TRue, the source parameter is a file path; otherwise, it is simply a string. See AlsoSee the ".NET Framework Regular Expressions," "FileStream Class," and "Stream-Reader Class" topics in the MSDN documentation. |