O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Create a String Consisting of a Repeated Character

Problem

You need to quickly create a string that consists of a single character repeated multiple times (for example, "------------").

Solution

Use the overloaded String constructor that accepts a single character and a repetition number.

Discussion

The following code creates a string made up of 100 dash characters. Note that in order to use the nondefault String constructor, you must use the New keyword when you declare the string.

Dim Dashes As New String("-"c, 100)

When specifying the character to repeat, you can append the letter c after the string to signal that the quoted text represents a Char, as required by this constructor, not a String. This is required if you have Option Strict enabled, which ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required