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.Copy and CopyTo

Use the String.Copy and CopyTo functions to perform string copying operations.
String.Copy, CopyTo. Usually a string does not need copying. We can use literals, or the strings returned by methods like Replace or ToString.Strings
With Copy, we copy an existing string and get a new string. With CopyTo we copy the characters of a string into a Char array. We specify an index and length.
Copy example. The String.Copy method receives a string and returns a copy of that string. This is a shared method—we do not call it on a string instance.Shared

Here: We use ReferenceEquals to see if two variables point to the same reference. String.Copy returns a separate, new string.

VB.NET program that uses String.Copy Module Module1 Sub Main() Dim value As String = "cat" ' Use String.Copy to copy a string. Dim valueCopied As String = String.Copy(value) ' The copy has a different reference. If Not ReferenceEquals(value, valueCopied) Then Console.WriteLine("Not same reference") End If End Sub End Module Output Not same reference
CopyTo. This method has nothing in common with String.Copy. CopyTo is an instance method and it copies the string's characters to a char array.

Here: We copy the first 3 characters in a string to a 3-element char array. We then use For-Each to display the array's contents.

For Each, For

Tip: In VB.NET we use the last array index to indicate its length. So a 2 means a 3-element array.

Array
VB.NET program that uses CopyTo Module Module1 Sub Main() Dim value As String = "dog" ' A 3-element char array. Dim destination(2) As Char ' Copy string to the destination array. value.CopyTo(0, destination, 0, 3) ' Loop over the resulting char array. For Each letter As Char In destination Console.WriteLine(letter) Next End Sub End Module Output d o g
A summary. The String.Copy and CopyTo methods provide ways to get the contents of strings and put them in new objects. CopyTo is more useful—it helps use char arrays.Char Array
As developers we rarely need to use String.Copy. But it is helpful to know it exists. To reduce memory usage, it is best to reuse existing strings.
© 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