Last active
July 2, 2017 06:55
-
-
Save abdulateef/b4bed36789d6f3a363000173290ddd36 to your computer and use it in GitHub Desktop.
how to count the number of columns in a text file using c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int columnCount(string filepath) | |
{ | |
int numColumns = 0; | |
using(var reader = File.OpenText(filepath)) | |
{ | |
while(reader.ReadLine() != null ) | |
{ | |
string data = System.IO.File.ReadAllText(filepath); | |
// split the text | |
data = data.Replace("\n", "\r"); | |
string[] lines = data.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries); | |
// to get how many rows and columns | |
//int numRows = lines.Length; | |
numColumns = lines[0].Split(',').Length; | |
} | |
} | |
return numColumns; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment