C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: In the B subroutine, we show how to create a String by repeating the letter "a" ten times.
Finally: In the third subroutine C, we construct a String instance from a range of a Char array.
Result: The program's execution shows that all the New String constructors worked as expected.
VB.NET program that uses New String constructor
Module Module1
Sub Main()
A()
B()
C()
End Sub
Sub A()
' Construct string from character array.
Dim array(2) As Char
array(0) = "a"c
array(1) = "b"c
array(2) = "c"c
Dim example As String = New String(array)
Console.WriteLine(example)
Console.WriteLine(example = "abc")
Console.WriteLine()
End Sub
Sub B()
' Construct string from repeated character.
Dim example As String = New String("a"c, 10)
Console.WriteLine(example)
Console.WriteLine(example = "aaaaaaaaaa")
Console.WriteLine()
End Sub
Sub C()
' Construct string from part of character array.
Dim array(5) As Char
array(0) = "a"c
array(1) = "B"c
array(2) = "c"c
array(3) = "D"c
array(4) = "e"c
array(5) = "F"c
Dim example As String = New String(array, 0, 3)
Console.WriteLine(example)
Console.WriteLine(example = "aBc")
End Sub
End Module
Output
abc
True
aaaaaaaaaa
True
aBc
True
Note: This means that VB.NET only has three overloads on the constructor, while the C# language presents eight.
Also: If you want to repeat a certain character many times, the String constructor can do that too.