Strings.Replace(String, String, String, Int32, Int32, CompareMethod) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a string in which a specified substring has been replaced with another substring a specified number of times.
public static string? Replace (string? Expression, string? Find, string? Replacement, int Start = 1, int Count = -1, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
public static string Replace (string Expression, string Find, string Replacement, int Start = 1, int Count = -1, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
static member Replace : string * string * string * int * int * Microsoft.VisualBasic.CompareMethod -> string
Public Function Replace (Expression As String, Find As String, Replacement As String, Optional Start As Integer = 1, Optional Count As Integer = -1, Optional Compare As CompareMethod = Microsoft.VisualBasic.CompareMethod.Binary) As String
Parameters
- Expression
- String
Required. String expression containing substring to replace.
- Find
- String
Required. Substring being searched for.
- Replacement
- String
Required. Replacement substring.
- Start
- Int32
Optional. Position within Expression
that starts a substring used for replacement. The return value of Replace
is a string that begins at Start
, with appropriate substitutions. If omitted, 1 is assumed.
- Count
- Int32
Optional. Number of substring substitutions to perform. If omitted, the default value is -1, which means "make all possible substitutions."
- Compare
- CompareMethod
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings for values.
Returns
One of the following values.
If | 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 | Nothing |
Count is 0 | Copy of Expression |
Exceptions
Count
< -1 or Start
<= 0.
Examples
This example demonstrates the Replace
function.
Dim TestString As String = "Shopping List"
' Returns "Shipping List".
Dim aString As String = Replace(TestString, "o", "i")
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.
The Compare
argument can have the following values.
Constant | Description |
---|---|
Binary |
Performs a binary comparison |
Text |
Performs a textual comparison |