You need to process many pieces of string data with more efficiency than is allowed using standard .NET Framework immutable strings.
Solution
The StringBuilder object provides extremely fast and efficient in-place processing of string and character data. The following code demonstrates several of its powerful methods and some of the techniques you can use to speed up your string processing:
Dim workText As New System.Text.StringBuilder
' ----- Build a basic text block.
workText.Append("The important")
workText.Append(vbNewLine)
workText.Append("thing is not")
workText.AppendLine()
workText.AppendLine("to stop questioning.")
workText.
Append("--Albert Einstein")
MsgBox(workText.ToString( ))
' ----- Delete the trailing text.
Dim endSize As Integer = "--Albert Einstein".Length
Recipe 5.2. Creating a String of N Identical Characters
Problem
You need to create a string comprised of a single character repeated many times. These strings are sometimes useful in the formatting of ASCII text for display or printed output.
Solution
Create a new string of repeated characters using the String class itself. One of its overloaded constructors accepts a character to repeat and a repetition count.
Recipe 5.3. Creating a String by Repeating a String N Times
Problem
You want a string comprised of a sequence of characters repeated many times.
Solution
Use a StringBuilder to append as many copies of the string as desired. Then convert the result to a true string using the StringBuilder's ToString() method:
You need to store a string in such a way that a user won't recognize it, but you also want to make sure that the string stays the same length and that it contains only printable ASCII characters.
Solution
Sample code folder: Chapter 05\ObfuscateString
Process each printable character of the string by shifting its ASCII value to that of another character within the same set. The following two functions can be used to obfuscate strings in this way and then return them to their original states:
Public Function Obfuscate(ByVal origText As String) As String
' ----- Make a string unreadable, but retrievable.
Dim textBytes As Byte( ) = _
System.Text.Encoding.UTF8.GetBytes(origText)
For counter As Integer = 0 To textBytes.Length - 1
Recipe 5.5. Converting Binary Data to a Hexadecimal String
Problem
You need to convert a byte array to a hexadecimal string. This is handy for the display or documentation of binary data.
Solution
Use a bit converter to get the hexadecimal representation of each byte within a block of data. The following code generates the hexadecimal string from source data:
Dim result As String = Replace(BitConverter.ToString( _
Recipe 5.6. Extracting Substrings from Larger Strings
Problem
You want to extract substrings located at the left end, the right end, or somewhere in the middle of a string.
Solution
Visual Basic 2005 strings now have a built-in method named Substring() that provides an alternative to the traditional Visual Basic functions Left(), Mid(), and Right(), although the language retains these features if you wish to use them. To emulate each of these functions, set the Substring() method's parameters appropriately. The following code shows how to do this:
Dim quote As String = "The important thing is not to " & _
You want to convert a string to all uppercase, all lowercase, or mixed case (with only the first letter of each word in uppercase).
Solution
Sample code folder: Chapter 05\MixedCase
The string methods ToUpper() and ToLower() make it easy to convert strings to upper-and lowercase, and a short special-purpose function can perform the mixed conversion. You can also use the standard Visual Basic UCase() and LCase() methods. To mix-case a string, use Visual Basic's StrConv() function.
Discussion
Changing strings to upper- or lowercase is standard Visual Basic fare:
' ----- To upper case.
newString = oldString.ToUpper()
newString = UCase(oldString)
' ----- To lower case.
newString = oldString.ToLower()
newString = LCase(oldString)
To convert the string to mixed or "proper" case, use one of the conversion methods included in the StrConv() function: