C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
Note: In many programs, there are strings that are immutable and will not change throughout the program.
And: I needed to know if the string matched one of two substrings. The code was critical.
Warning: From a high-level perspective, hard-coding multiple character compares is a poor design.
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;
}
And: We see that the unwound loops, those that test chars individually, are much faster.
ForBenchmark results
StartsWith and EndsWith methods: 2176 ms
Char testing: 586 ms [faster]