HomeSearch

C# Char Test (If Char in String Equals a Value)

This C# article shows a fast way to test char values. It sees if a char in a string equals a value.
Char test. Consider a string. We can test it with complex methods with StartsWith or EndsWith. But if we test chars directly, this can be faster.StringsStartsWith, EndsWithChar
Specialized logic. We can also arrange if-tests with chars to avoid repeated work—for example, if we want to test multiple strings starting with a letter, we can test that value once.If
A simple example. Here is a simple example of testing the first letter in a string. Here we see if the string equals the lowercase letter "t."
C# program that uses char test using System; class Program { static void Main() { string value = "test"; // See if first char is the lowercase T. if (value[0] == 't') { Console.WriteLine("First char is t"); } } } Output First char is t
Some optimization. String comparisons can be optimized. We need to test a string thousands to billions of times. We benchmark an alternative way of comparing constant strings.Benchmark
Methods. In many programs, there are strings that are immutable and will not change throughout the program. I needed to know if the string matched one of two substrings.

Note: The code was critical. It was in the hot path of this application. I optimized several places to manually check characters.

Code that uses EndsWith, A: C# static bool EndsThreeA(string s) { return s.EndsWith("three", StringComparison.Ordinal); } Code that uses char test, B: C# static bool EndsThreeB(string s) { int len = s.Length; if (len >= 5 && s[len - 5] == 't' && s[len - 4] == 'h' && s[len - 3] == 'r' && s[len - 2] == 'e' && s[len - 1] == 'e') { return true; } else { return false; } } Code that uses StartsWith, A: C# static bool StartsMsA(string s) { return s.StartsWith("http://microsoft.com", StringComparison.Ordinal) || s.StartsWith("http://msdn.microsoft.com", StringComparison.Ordinal); } Code that uses char test, B: C# static bool StartsMsB(string s) { int len = s.Length; if (len > 19 && s[0] == 'h' && s[1] == 't' && s[2] == 't' && s[3] == 'p' && s[4] == ':' && s[5] == '/' && s[6] == '/' && s[7] == 'm') { if (s[8] == 'i' && s[9] == 'c' && s[10] == 'r' && s[11] == 'o' && s[12] == 's' && s[13] == 'o' && s[14] == 'f' && s[15] == 't' && s[16] == '.' && s[17] == 'c' && s[18] == 'o' && s[19] == 'm') { return true; } if (len > 24 && s[8] == 's' && s[9] == 'd' && s[10] == 'n' && s[11] == '.' && s[12] == 'm' && s[13] == 'i' && s[14] == 'c' && s[15] == 'r' && s[16] == 'o' && s[17] == 's' && s[18] == 'o' && s[19] == 'f' && s[20] == 't' && s[21] == '.' && s[22] == 'c' && s[23] == 'o' && s[24] == 'm') { return true; } return false; } return false; }
Discussion. From a high-level perspective, hard-coding multiple character compares is a poor design. However, the computer doesn't care about code elegance.

Note: Consider the above code. If the first check fails, it doesn't repeat itself on the first 8 letters.

Benchmark. The "A" methods use StartsWith and EndsWith, and the StringComparison.Ordinal option. The "B" methods individually test chars.

And: We see that the unwound loops, those that test chars individually, are much faster.

For
Benchmark of testing chars StartsWith and EndsWith methods: 2176 ms Char testing: 586 ms [faster]
Summary. We tested individual chars in C# strings. By testing chars separately, we also avoid redundant work. This would be an excellent use for a code-generation tool.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls