Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Last active July 2, 2017 06:55
Show Gist options
  • Save abdulateef/b4bed36789d6f3a363000173290ddd36 to your computer and use it in GitHub Desktop.
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#
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