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.
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.
Most of the time you create string variables using the default constructor, which initializes the variables to Nothing. This is why you must assign a string value to a string variable after creating it, but before using its contents. However, you can use over-loaded versions of the string constructor to assign string data immediately upon creation. One version of the string constructor takes a character and a count and efficiently builds a string by repeating the character the given number of times. The following statement builds a string of 72 asterisks:
Dim lotsOfAsterisks As New String("*"c, 72)
Visual Basic 2005 also provides a second way to create strings of duplicated characters. The StrDup() function, which is very similar to the original String() function found in Visual Basic 6, does the trick:
lotsOfAsterisks = StrDup(72, "*")
Notice the difference in the order of the parameters between the string constructor syntax and the function call. Fortunately, Visual Studio's IntelliSense means you don't have to memorize the order of the parameters.
Recipe 5.45 demonstrates another method of creating strings of a common character.