Tag Archives: strings

Use digits of Pi to draw a picture in C#

The article 10 stunning images show the beauty hidden in pi shows several ways to use Pi to generate pictures. This example demonstrates one of them. The program starts drawing at the origin (0, 0). It then scans through the … Continue reading

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

Look for dates in the digits of Pi in C#

The example Find patterns within pi in C# looks for patterns within the digits of Pi. You can use that example to look for dates such as 4/1. This example determines whether the first 100 thousand digits of Pi contains … Continue reading

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

Find patterns within Pi in C#

March 14 is known as Pi Day because its numeric representation 3/14 includes the first three digits of Pi: 3.14. (At least in the United States. In Europe where they usually write dates month first, Pi Day is July 22 … Continue reading

Posted in algorithms, mathematics, strings | Tagged , , , , , , , , , , , , , | 1 Comment

Make a case-insensitive dictionary in C#

The example Use a Dictionary to store and retrieve items in C# shows how to use a dictionary to store and retrieve values with keys. By default the Dictionary class is case-sensitive so the keys “Rod Stephens” and “rod stephens” … Continue reading

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

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