In the Microsoft.VisualBasic.FileIO
(Microsoft.VisualBasic.dll) namespace lives the TextFieldParser
class.
This useful little class can be used to parse structured text files – either delimited or fixed length. You can iterate over the lines in the file and extract the data through the ReadFields
method.
Since it is provided by Microsoft, you can use it in environments that do not allow “third-party” libraries and as it is a .NET libarary you can use it in any .NET language (yes, C# and F# included) – just import the library.
The examples on MSDN are all in VB.NET, but are easily translated to other .NET dialects.
– How to: Read From Comma-Delimited Text Files
– How to: Read From Fixed-width Text Files
– How to: Read From Text Files with Multiple Formats
Although I don’t like Visual Basic syntax, there seem to be a lot of goodies in Microsoft.VisualBasic namespaces. I was thinking the other day how great it would be if the FCL included a utility to split lines in a way similar to awk, and this looks pretty close. Thanks for pointing it out!
Congrats on your new job at StackOverflow. It. Just. Makes. Sense.
The only problem is performance.
It may be a problem. You are making a sweeping statement here. Performance issues should be tackled if they are an issue (and only after measurements have shown where the bottlenecks are). There are faster parsers out there, but not built in.