Syntax
Replace (expression, find, replacewith [, compare [, count [, start])
The syntax of the Replace function includes the following parameters:
Parameter description
Expression is required. The string expression contains the substring to be replaced.
Find is required. The substring to be searched.
Replacewith is required. The substring used for replacement.
Start is optional. Expression to start searching for the position of the substring. If omitted, the default value is 1. Must be used when associating with count
Count is optional. Number of substrings to replace. If this parameter is omitted, the default value is-1, indicating all possible replacements. It must be used in association with start.
Compare is optional. Indicates the value of the comparison type used to calculate the substring. For values, see the "Settings" section. If omitted, the default value is 0, which means binary comparison is required.
Set
The compare parameter can have the following values:
Constant value description
VbBinaryCompare 0 performs binary comparison.
VbTextCompare 1 performs text comparison.
Return value
Replace returns the following values:
If Replace returns
Expression is a zero-length, zero-length string ("").
Expression is Null.
Find a zero-length expression copy.
Replacewith is a zero-length expression copy, in which all content specified by the find parameter is deleted.
Start> Len (expression) zero-length string.
Count is a copy of 0 expression.
Description
The return value of the Replace function is a string after replacement (starting from the position specified by start to the end of the expression string), rather than a copy of the original string from start to end.
The following example uses the Replace function to return a string:
| The code is as follows: |
Copy code |
|
Dim MyString
MyString = Replace ("XXpXXPXXp", "p", "Y") ''binary comparison starts from the left end of the string. Returns "XXYXXPXXY ".
MyString = Replace ("XXpXXPXXp", "p", "Y", ''the text comparison starts with the third character. Returns "YXXYXXY ". 3,-1, 1)
String str = "asdasdzxc ";
Console. WriteLine (str. Replace ('A', '1 ')); |
Instance
| The code is as follows: |
Copy code |
|
Private string formatString (string str)
{
Str = str. Replace ("", "& nbsp;"); // process spaces (only two spaces are allowed)
Str = str. Replace ("<", "& lt;"); // handle minor signs
Str = str. Replace (">", "& gt;"); // process the mark
Str = str. Replace ("n", "<br/>"); // process line breaks
Return str;
} |
More better instances view http://www.111cn.net/net/c/38795.htm