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 Insert String

Use the Insert Function to add string data at an index to an existing String.
Insert. This Function places another String somewhere inside the first String. Using the Insert function on the String type, we create a longer String.Strings
Notes, strings. When we call Insert, a new string is created—the existing string is not modified. Insert() also is available on StringBuilder, which does modify an underlying buffer.StringBuilder
Insert example. We call Insert. We must assign a variable to the result of Insert to get the updated string. A new string is created upon the managed heap.

Argument 1: This is the index in the original string where we want to place the new substring. With 4, we place after the fourth char.

Argument 2: This is the new string we want to place inside of the original string. It will be found in the result.

VB.NET program that uses Insert function on String Module Module1 Sub Main() Dim value As String = "the cat" Console.WriteLine("INITIAL: {0}", value) ' Use Insert at an index. Dim result As String = value.Insert(4, "soft ") Console.WriteLine("INSERT: {0}", result) End Sub End Module Output INITIAL: the cat INSERT: the soft cat
Insert, IndexOf. We can use the IndexOf function with Insert. This demonstrates how we can search for the target index before passing that value to Insert.IndexOf

Insert: The first argument to Insert here is the index we received from IndexOf.

And: The index is the position of the start of the substring "cat" in the original string.

VB.NET program that uses Insert with IndexOf Module Module1 Sub Main() Dim value As String = "the soft cat" Console.WriteLine("INITIAL: {0}", value) ' Use Insert with IndexOf. Dim index As Integer = value.IndexOf("cat") Dim result As String = value.Insert(index, "orange ") Console.WriteLine("INSERT: {0}", result) End Sub End Module Output INITIAL: the soft cat INSERT: the soft orange cat
A summary. Strings are immutable. When we call Insert we must assign a variable reference to its result. Despite this possible complication, the Insert function is useful in many programs.
© 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