VSFlexString Control > VSFlexString Properties, Events, and Methods > VSFlexString Properties > Replace Property |
Sets a string to replace all matches.
[form!]VSFlexString.Replace = value As String
The replacement occurs as soon as you assign the new text to the Replace property. To perform the replacement on several strings, you must set both the Text and Replace properties for each string you want to modify.
For example:
fs.Text = "The quick brown fox jumped over the lazy dog."
fs.Pattern = " [fd][a-z]*"
fs.Replace = " animal"
Debug.Print fs.Text
This code produces the following output:
The quick brown animal jumped over the lazy animal.
The Replace property is particularly useful for changing marked up text documents such as HTML or XML. For example, the code below converts bold HTML text into bold underlined text:
fs = "<P>This is some <strong>HTML</B> text.</P>"
fs.CaseSensitive = False
fs.Pattern = "<strong>"
fs.Replace = "<strong><U>"
fs.Pattern = "</B>"
fs.Replace = "</U></B>"
Debug.Print fs
This code produces the following output:
<P>This is some <strong><U>HTML</U></B> text.</P>
The Replace string may contain tags, specified using curly brackets with the tag number between them, e.g., "{n}". The tags expand into the portions of the original Text string that were matched to the corresponding tags in the search Pattern. The example below illustrates this:
' set up a pattern to search for a filename and extension:
' the curly brackets define two tags
' (note how the period is escaped with a backslash)
fs.Pattern = "{[A-Za-z0-9_]+}\.{...}"
' assign a string to be matched against the pattern
' tag 0 will match the filename, tag 1 the extension
fs.Text = "AUTOEXEC.BAT"
' expand the string (note that each tag may be used several times)
fs.Replace = "File {0}.{1}, Name: {0}, Ext: {1}"
Debug.Print fs.Text
This code produces the following output:
File AUTOEXEC.BAT, Name: AUTOEXEC, Ext: BAT
String