Replace Function

   
Replace Function

Class

Microsoft.VisualBasic.Strings

Syntax

 Replace(   expression, find, replace   [, _   start   [,   count   [,   compare   ]]]) 
expression (required; String)

The complete string containing the substring to be replaced

find (required; String)

The substring to be found by the function

replace (required; String)

The new substring to replace find in expression

start (optional; Long)

The character position in expression at which the search for find begins

count (optional; Long)

The number of instances of find to replace

compare (optional; CompareMethod constant)

The method used to compare find with expression ; its value can be CompareMethod.Binary (for case-sensitive comparison) or CompareMethod.Text (for case-insensitive comparison)

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

expression = ""

Zero-length string ("")

find = ""

Copy of expression

replace = ""

Copy of expression with all instances of find removed

start > Len( expression )

Zero-length string ("")

count = 0

Copy of expression

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.

  • CompareMethod.BinaryCompare is case sensitive; that is, Replace matches both character and case, whereas CompareMethod.Text is case insensitive, matching only character regardless of case.

  • The default value for compare is CompareMethod.Binary .

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

Programming Tips and Gotchas

  • If count is not 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" Console.WriteLine(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, _  CompareMethod.Text) 

    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 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.

  • The BCL's System.String class also has a public instance Replace method, which replaces all occurrences of a character or string with another. Its syntax is:

       sString   .Replace(oldValue,   newValue   ) 

    where oldValue is a String or Char value containing the text to be replaced and newValue is a String or Char value containing the replacement text.

See Also

InStr Function, InStrRev Function, Mid Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net