TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET String Constructor (New String)

Use the String instance constructor to create new strings. Test different arguments.
String has a constructor. This allows you to convert a Char array or single char into a String instance. With the New String syntax, you can create String instances much faster than with the equivalent loop constructs.Strings
Example. First, this program is divided into three important routines. These are the A, B, and C subroutines. In the A subroutine, we show how to create a New String from an entire character array.

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
Discussion. The string constructor is different in the C# language and the VB.NET language. The VB.NET language does not allow you to use the unsafe (pointer-based) methods. The C# language allows you to.

Note: This means that VB.NET only has three overloads on the constructor, while the C# language presents eight.

Summary. The String constructor in the VB.NET language provides an essential way to create certain kinds of strings. If you need a string that is based on a range of Char array, the String constructor is ideal.

Also: If you want to repeat a certain character many times, the String constructor can do that too.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf