Remove a string - VB.NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

The following code removes a string at a specific location. In other words, the following code is used to remove a letter or even a word from a sentense.

We have a sentence: My name is Dominic Eric Alphonso. We like to remove the middle name from it.

Dim FirstName As String = "Dominic"
Dim MiddleName As String = "Eric"
Dim LastName As String = "Alphonso"
Dim FullName As String
FullName = "My name is " & FirstName & " " & MiddleName & " " & LastName
FullName = FullName.Remove(19, MiddleName.Length)
MsgBox(FullName)

Notice in the above code we started removing at index 19 of the sentense (position of the first letter of the middle name), and we removed 4 letters after that (The length of the middle name).
Output:
My name is Dominic Alphonso
To remove the last name:
FullName = FullName.Remove(24, LastName.Length)

Note: When you count to get the position of a letter or a string, starting counting from 0 and make sure to count all charachters including blank spaces.


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.