Category Archives: strings

Understand string equality testing in C#

To really understand this example, you need to know about string interning, equality testing, and operator overloading. Interning First, interning. To save space, .NET uses an intern pool to store literal strings at compile time. If the program contains a … Continue reading

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

Use a Dictionary to store and retrieve items in C#

This example demonstrates a Dictionary. A Dictionary is basically a strongly typed Hashtable. A Hashtable stores key/value pairs in a way that makes looking up values extremely fast. For example, suppose you have an employee database that uses EmployeeId values … Continue reading

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

Compare the performance of string comparison methods in C#

This example uses the following code to make four strings: string value0 = “AAAAAAAAAAAAAAAAAAAAAA”; string value1 = “AAAAAAAAAAAAAAAAAAAAAA”; string value2 = “AAAAAAAAAAAAAAAAAAAAAB”; string value3 = “BAAAAAAAAAAAAAAAAAAAAA”; It then uses several methods for comparing the first string with the others. For … Continue reading

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

Make a hangman game in C#

Special thanks to Jeff Scarterfield for the skeleton drawing used by the program. This example builds a simple hangman game that uses the dictionary created by the example Use LINQ to select words of certain lengths from a file in … Continue reading

Posted in algorithms, files, games, graphics, strings | Tagged , , , , , , , , , , , , | Leave a comment

Remove non-printable ASCII characters from a string in C#

The following TrimNonAscii extension method removes the non-printable ASCII characters from a string. public static string TrimNonAscii(this string value) { string pattern = “[^ -~]+”; Regex reg_exp = new Regex(pattern); return reg_exp.Replace(value, “”); } In ASCII, the printable characters lie … Continue reading

Posted in extension methods, strings | Tagged , , , , , , , , , , , , | 7 Comments

Make a TextBox convert text into proper case in C#

The example Convert a string to proper case (title case) in C# uses a CultureInfo object’s TextInfo property to convert text into proper case (or title case). There are a couple of tricks and potential problems that you need to … Continue reading

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

Convert a string to proper case (title case) in C#

In proper case (or title case) the first letter of each word is capitalized. This is actually pretty easy once you know where to find the method that converts to proper case. The example uses the following code to convert … Continue reading

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

Make TextBoxes automatically convert case in C#

It’s actually quite easy to make a TextBox convert case in .NET. Simply set the TextBox control’s CharacterCasing property to Lower or Upper. Then the control automatically converts alphabetic characters into the correct case. You can do this at design … Continue reading

Posted in controls, strings, user interface | Tagged , , , , , , , , , , , , | Leave a comment

Make a stopwatch in C#

This program is a simple stopwatch. When you click the Start button, the program changes the button’s caption to Stop and starts a Timer. As the stopwatch Timer runs, the program displays the elapsed time. Click the Stop button to … Continue reading

Posted in internationalization, miscellany, strings | Tagged , , , , , , , , , , , , , | Leave a comment

Display map distance with limited significant digits in C#

The post Measure distance on a map with a scale in C# lets you measure a distance on a map. However, it displays too many significant digits in the results to look nice. For example, the sample picture displays the … Continue reading

Posted in algorithms, extension methods, graphics, mathematics, strings | Tagged , , , , , , , , , , , , , , , , | Leave a comment