Strings.Replace Method

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

Namespace: Microsoft.VisualBasic
Assembly: Microsoft.VisualBasic (in microsoft.visualbasic.dll)

static String^ Replace (
	String^ Expression, 
	String^ Find, 
	String^ Replacement, 
	[OptionalAttribute] int Start, 
	[OptionalAttribute] int Count, 
	[OptionalAttribute] CompareMethod Compare
public static String Replace (
	String Expression, 
	String Find, 
	String Replacement, 
	/** @attribute OptionalAttribute() */ int Start, 
	/** @attribute OptionalAttribute() */ int Count, 
	/** @attribute OptionalAttribute() */ CompareMethod Compare
public static function Replace (
	Expression : String, 
	Find : String, 
	Replacement : String, 
	Start : int, 
	Count : int, 
	Compare : CompareMethod
) : 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.

Return Value

If Find is zero-length or Nothing, Replace returns copy of Expression. If Replace is zero-length, Replace returns copy of Expression with no occurrences of Find. If Expression is zero-length or Nothing, or Start is greater than length of Expression, Replace returns Nothing. If Count is 0, Replace returns copy of Expression.

For more detailed information, see the Visual Basic topic Replace Function (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.

The Compare argument can have the following values.




Performs a binary comparison


Performs a textual comparison

This example demonstrates the Replace function.

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions