This documentation is archived and is not being maintained.

Replace Function (Visual Basic)

Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Public Function Replace(
   ByVal Expression As String,
   ByVal Find As String,
   ByVal Replacement As String,
   Optional ByVal Start As Integer = 1,
   Optional ByVal Count As Integer = -1,
   Optional ByVal Compare As CompareMethod = CompareMethod.Binary
) As String


Required. String expression containing substring to replace.


Required. Substring being searched for.


Required. Replacement substring.


Optional. Position within Expression where substring search is to begin. If omitted, 1 is assumed.


Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means "make all possible substitutions."


Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings for values.

The Compare argument can have the following values.




Performs a binary comparison


Performs a textual comparison

Replace returns the following values.


Replace returns

Find is zero-length or Nothing

Copy of Expression

Replace is zero-length

Copy of Expression with no occurrences of Find

Expression is zero-length or Nothing, or Start is greater than length of Expression


Count is 0

Copy of Expression

Exception type

Error number




Count < -1 or Start <= 0.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

The return value of the Replace function is a string that begins at the position specified by Start and concludes at the end of the Expression string, with the substitutions made as specified by the Find and Replace values.

This example demonstrates the Replace function.

Dim TestString As String = "Shopping List"
' Returns "Shipping List".
Dim aString As String = Replace(TestString, "o", "i")

Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)