Visual Basic Language Reference  

Replace Function

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

Parameters

Expression
Required. String expression containing substring to replace.
Find
Required. Substring being searched for.
Replacement
Required. Replacement substring.
Start
Optional. Position within Expression where substring search is to begin. If omitted, 1 is assumed.
Count
Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means make all possible substitutions.
Compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings for values.

Settings

The Compare argument can have the following values:

Constant Description
Binary Performs a binary comparison
Text Performs a textual comparison

Return Values

Replace returns the following values:

If Replace returns
Expression is zero-length Zero-length string ("")
Find is zero-length Copy of Expression
Replace is zero-length Copy of Expression with no occurrences of Find
Start is greater than length of expression Zero-length string
Count is 0 Copy of Expression

Exceptions/Errors

Exception type Error number Condition
ArgumentException 5 Count < -1 or Start <= 0.

Remarks

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.

Example

This example demonstrates the Replace function.

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

See Also

Filter Function