Tag Archives: parse

Parse file sizes in KB, MB, GB, and so forth in C#

The example Format file sizes in KB, MB, GB, and so forth in C# shows how to convert a number into a string formatted in KB, MB, etc. This example does the opposite: it parses a value such as “1.23 … Continue reading

Posted in algorithms, files, mathematics | Tagged , , , , , , , , , | Leave a comment

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

Replace text in the lines of a string in C#

This example uses the following code to replace text in the lines of a string. private void btnGo_Click(object sender, EventArgs e) { try { Regex reg_exp = new Regex(txtPattern.Text); lblResult.Text = reg_exp.Replace( txtInput.Text, txtReplacementPattern.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); … Continue reading

Posted in regular expressions, strings | Tagged , , , , , , , , , , , , | 6 Comments

Use TryParse to parse values entered by the user in C#

The example Parse user-entered values in C# explains how you can use a try catch block to protect against errors when parsing values entered by the user. For example, if the user types “ten” in a TextBox where you expect … Continue reading

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

Parse user-entered values in C#

You can use a data type’s Parse method to convert text entered by the user into a value with that data type. For example, the following code converts the text in the TextBox named txtInt into an int. // Parse … Continue reading

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