Replace Function (VB6)

Named Arguments

No

Syntax

Replace(string, stringToReplace, replacementString [, _
        start[, count[, compare]]])


string

Use: Required

Data Type: String

The complete string containing the substring to be replaced.


stringToReplace

Use: Required

Data Type: String

The substring to be found by the function.


replacementString

Use: Required

Data Type: String

The new substring to replace stringToReplace in string.


start

Use: Optional

Data Type: Long

The character position in string at which the search for stringToReplace begins.


count

Use: Optional

Data Type: Long

The number of instances of stringToReplace to replace.


compare

Use: Optional

Data Type: VbCompareMethod constant

The method that compares stringToReplace with string ; its value can be vbBinaryCompare, vbTextCompare, or vbDatabaseCompare.

Return Value

The return value from Replace depends on the parameters you specify in the argument list, as the following table shows:

If Return Value
string = "" Zero-length string ("")
string is Null An error
StringToReplace = "" Copy of string
replacementString = "" Copy of string with all instances of stringToReplace removed
start > Len(string) Zero-length string ("")
count = 0 Copy of string

Description

Replaces a given number of instances of a specified substring in another string.

Rules at a Glance

  • If start is omitted, the search begins at the start of the string.

  • If count is omitted, all instances of the substring after start are replaced.

  • vbBinaryCompare is case sensitive; that is, Replace matches both character and case, whereas vbTextCompare is case insensitive, matching only character, regardless of case.

  • The default value for compare is vbBinaryCompare.

  • start not only specifies where the search for stringToReplace begins, but also where the new string returned by the Replace function commences.

Programming Tips and Gotchas

  • If count isn't used, be careful when replacing short strings that may form parts of unrelated words. For example, consider the following:

    Dim sString
    sString = "You have to be careful when you do this " _
               & "or you could ruin your string"
    Debug.Print Replace(sString, "you", "we")

    Because we don't specify a value for count, the call to Replace replaces every occurrence of "you" in the original string with "we." But the fourth occurrence of "you" is part of the word "your," which is modified to become "wer."

  • You must also be aware that if start is greater than 1, the returned string starts at that character, and not at the first character of the original string, as you might expect. For example, given the statements:

    sOld = "This string checks the Replace function"
    sNew = Replace(sOld, "check", "test", 5, _
                   vbTextCompare)

    sNew will contain the value

    "string tests the Replace function"

  • You can use the Mid function on the left side of an argument to replace a part of string, but to replace more than one instance of a substring requires a complicated Do While loop that constantly checks for the position of any remaining instances of the substring to be replaced.

See Also

Mid Function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset