Category 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

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

Convert an integer into an ordinal (1st, 2nd, 3rd) in C#

This example uses the following int extension method to convert an integer into an ordinal. The method returns an integer’s ordinal suffix, as in “rd” fo rthe value i1103, so you can make 1103rd or 1,103rd. // Return the int’s … Continue reading

Posted in parsing, strings | Tagged , , , , , , , , , , , , , , | 1 Comment

Convert bases decimal, hexadecimal, octal, and binary in C#

It’s easy to convert bases if you use the Convert class’s ToInt64 and ToString methods. For example, the following statement parses the text in the TextBox named source and saves the result in the long variable value. The “16” means … Continue reading

Posted in mathematics, parsing | Tagged , , , , , , , , , , , | 4 Comments