VB string replace

In VB, you have three ways to replace an old sub-string of a string with a new string:
1. using Replace(string1, old_sub-string, new_string,[start],[count],[CompareMethod]) function
-string1 is a string that contains the sub-string to be replaced.
-old_sub-string is the sub-string (of string1) to be replaced.
-new_string is the string to replace the old_sub-string.
-start is optional. Its default value 1. It specifies the where to start searching for the sub-string.
-count is optional. It is the number of times to replace the sub-string. If it is omitted, all sub-strings that are matched are repalced.
-CompareMethod is an optional argument. I takes one of the two constant values--CompareMethod.Binary or CompareMethod.Text. If you specify CompareMethod.Binary, the comparison is case-sensitive. If case-sensitivity is not important, you will use CompareMethod.Text instead of CompareMethod.Binary.
Example:
   Sub Main()
     Dim string1 As String = "This text contains sub-string to be replaced. Only the first sub-string is replaced in this example."
     Dim restr = Replace(string1, "sub-string", "VB", 1, 1) 'replace sub-string with VB
     Console.WriteLine(restr)
     Console.ReadLine()
   End Sub

2. using Replace(oldstring,newstring) or Replace(oldchar,newchar) method of the string class. By using this

method, all sub-strings found are repalced.
Example:

   Sub Main()
     Dim string1 As String = "This text contains sub-string to be replaced. All sub-strings are replaced in this example."
     Dim restr = string1.Replace("sub-string", "VB") 'replace sub-string with VB
     Console.WriteLine(restr)
     Console.ReadLine()
   End Sub

3. using the Replace(string1, pattern, replacewith) method of the Regex class.
-string1 contains sub-strings to be replace.
-pattern is a regular expression to match.
-replacewith is the new string to replace the sub-strings.

Example:
Imports System.Text.RegularExpressions
Module module1
   Sub Main()
     Dim string1 As String = "vb123programming4567language"
     Dim Str = Regex.Replace(string1, "\d+", " ") 'replace all digits with whitespaces
     Console.WriteLine(Str)
     Console.ReadLine()
   End Sub
End Module
The pattern "\d+" is to match at least one digit in the string. If you want to match at least one symbol in the string the pattern should be "\W+".
Example:
Imports System.Text.RegularExpressions
Module module1
   Sub Main()
     Dim string1 As String = "vb#@programming-()=language"
     Dim Str = Regex.Replace(string1, "\W+", " ") ' replace all symbols with whitespaces
     Console.WriteLine(Str)
     Console.ReadLine()

   End Sub
End Module

Posted by: Dara | post date: 02-15-2013 | Subject: VB.NET Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.