Tag Archives: parsing

Use a TextFieldParser to read fixed-width data in C#

This example uses a TextFieldParser object to load fixed-width data from a file that contains names and addresses. Each field has a fixed width. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program should discard … Continue reading

Posted in classes, database, files, parsing | Tagged , , , , , , , , , , , , , , , | Leave a comment

Use a TextFieldParser to read delimited data in C#

This example uses a TextFieldParser object to parse a data file that contains name and address data. The data contains fields delimited by commas and semi-colons. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program … Continue reading

Posted in classes, database, files, parsing | Tagged , , , , , , , , , , , , , | 2 Comments

Check text data type in C#

This example shows how to use the TryParse method to check text data type. Each data type (int, float, bool, etc.) has a TryParse method attempts to parse a string and returns true if it is successful. You can use … Continue reading

Posted in strings, variables | Tagged , , , , , , , , , , , , , , , , , , , , | Leave a comment

Convert enum values to and from strings in C#

The System.Enum class provides several methods that convert enum values to and from strings. Some particularly useful methods include: GetName Returns the string name of an enum value. GetNames Returns an array of strings holding the names of an enum‘s … Continue reading

Posted in enums, syntax | Tagged , , , , , , , , , , , , | 3 Comments

Remove non-digits or non-letters from a string in C#

Sometimes you might want to extract only the digits, letters, or some other group of characters from a string. You could loop through the string examining each character individually. Fortunately there’s a much easier way to do this. The regular … Continue reading

Posted in parsing, regular expressions, strings | Tagged , , , , , , , , , , , , , , , | Leave a comment

Add a ToString extension method to the TimeSpan structure in C#

Until .NET Framework version 4.0, you could not pass the TimeSpan structure’s ToString method a format string, so the result was always in the form hh:mm:ss.fffffff where hh is hours, mm is minutes, ss is seconds, and fffffff is 7 … Continue reading

Posted in extension methods, parsing | Tagged , , , , , , , , , , | 2 Comments

Find elapsed time in years, months, days, hours, minutes, and seconds in C#

The TimeSpan structure represents an elapsed time. It has Days, Hours, Minutes, Seconds, and Milliseconds properties that tell you about the time that has passed. For example, if two dates are 3 days and 4 hours apart, then Days = … Continue reading

Posted in parsing, variables | Tagged , , , , , , , , , , , , , , , , , | Leave a comment

Find the days, hours, minutes, or seconds between two dates in C#

This example uses a TimeSpan to find the days, hours, minutes, or seconds between two dates. When the user changes the date text, the program executes the following code. // If the text is a date, display // the elapsed … Continue reading

Posted in parsing, variables | Tagged , , , , , , , , , , , , , | Leave a comment

Split and join strings in C#

This example shows how you can use string methods to split and join strings. The string class’s Split method splits a string into pieces separated by delimiters. Different overloaded versions let you pass in an array of delimiters to use, … Continue reading

Posted in parsing, strings, variables | Tagged , , , , , , , , , , , | Leave a comment

Convert dates into an ordinal format (as in August 20th, 2020) in C#

This example shows how you can Convert dates into an ordinal format as in August 20th, 2020. The example Convert an integer into an ordinal (1st, 2nd, 3rd) in C# explains how to find the ordinal extension for an integer. … Continue reading

Posted in parsing, strings | Tagged , , , , , , , , , , , , , , , | Leave a comment