Strings.Replace(String, String, String, Int32, Int32, CompareMethod) 方法

定义

返回一个字符串,其中的指定子字符串已由另一个子字符串替换了指定的次数。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);
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

参数

Expression
String

必需。Required. 字符串表达式,包含要替换的子字符串。String expression containing substring to replace.

Find
String

必需。Required. 搜索的子字符串。Substring being searched for.

Replacement
String

必需。Required. 替换用的子字符串。Replacement substring.

Start
Int32

可选。Optional. Expression 内部要替换的子字符串的开始的位置。Position within Expression that starts a substring used for replacement. Replace 的返回值是字符串,在 Start 开始,具有相应的子情况。The return value of Replace is a string that begins at Start, with appropriate substitutions. 如果省略,则假定为 1。If omitted, 1 is assumed.

Count
Int32

可选。Optional. 执行子字符串替换的数目。Number of substring substitutions to perform. 如果省略,则默认值是 -1,表示“进行所有可能的替换”。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.

返回

Replace 返回下列值。Replace returns the following values.

如果If Replace 返回Replace returns
Find 的长度为零或 NothingFind is zero-length or Nothing Expression 的副本Copy of Expression
Replace 的长度为零Replace is zero-length Expression 的副本,不带 Find 匹配项Copy of Expression with no occurrences of Find
Expression 的长度为零或 Nothing,或者 Start 大于 Expression 的长度Expression is zero-length or Nothing, or Start is greater than length of ExpressionNothing
Count 是 0Count is 0 Expression 的副本Copy of Expression

异常

Count < -1 或 Start <= 0。Count < -1 or Start <= 0.

示例

此示例演示 Replace 函数。This example demonstrates the Replace function.

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

注解

Replace 函数的返回值是一个字符串,该字符串从 Start 指定的位置开始,在 Expression 字符串的末尾结束,并通过 FindReplace 值指定的替换进行。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.

Compare 参数可以具有下列值。The Compare argument can have the following values.

返回的常量Constant 描述Description
Binary 执行二进制比较Performs a binary comparison
Text 执行文本比较Performs a textual comparison

适用于

另请参阅