Game Development Reference
The unification of text storage and manipulation into a single String data type was
an excellent decision, although the .NET Framework is missing a way to efficient-
ly reverse the contents of a string. String reversal is an uncommon activity but not
extremely rare. There are a number of ways to accomplish string reversal, such as
appending each string character to a StringBuilder in reverse order, generating a
character array and calling Array.Reverse , or calling the StrReverse method in the
Microsoft.VisualBasic.Strings library. All three methods will perform the task, but
they are not the most efficient way to accomplish it.
The fastest way to perform string reversal is by using a character array with each
character from the input string appended to the array in reverse order, afterwards
constructing a new string from the reversed character array.
The following code shows how to do this.
string ReverseString(string input)
chars chars = new char[input.Length];
int index1 = input.Length -1;
int index2 = 0;
while (index1 >= 0)
chars[index1—] = input[index2++];
return new string(chars);
Compiling Regular Expressions
In a nutshell, regular expressions are a very powerful text manipulation tool that
compresses verbose and suboptimal text manipulation and matching patterns into
a couple of lines composing an efficient regular expression. The .NET framework
provides a number of robust classes for working with regular expressions, like the
Regex type that exists in the System.Text.RegularExpressions namespace. Regex pro-
vides a mechanism to execute a regular expression against a text string. When a
regular expression is set on the Regex object, it is converted to a partially compiled
representation, which is cached for execution during the application lifetime.