locked
fwi: replace(str... not the same as str.replace( RRS feed

  • Question

  • User-624338019 posted

     just found out that replace(string, ... does not do the same as string.replace(...
    when my string variable was an empty str the first one returns "nothing" and the second returns an empty string (as expected).

    just sloppy coding anyhow, it was a function that was converted from classic asp and never rewritten properly.
    example
    dim str as string = ""
    str = Replace(str, "'", "") 'makes str = nothing
    str = str.Replace("'", "") ' makes str = ""

    Friday, October 2, 2009 8:50 AM

Answers

  • User-952121411 posted

     Yes that is correct.  'Replace' comes from the Microsoft.VisualBasic library and has a different way of processing the string.  Read this about the 'Replace' VB method from the MSDN:

    "If Expression is zero-length or Nothing, or Start is greater than length of Expression The 'Replace' returns 'Nothing'"

    Replace Function (Visual Basic):

    http://msdn.microsoft.com/en-us/library/bt3szac5.aspx

    String.Replace is from the core System library and here are the comments from the MSDN on the return value:

    "A String equivalent to this instance but with all instances of oldValue replaced with newValue."

    String.Replace Method (String, String):

    http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

     

    So as you can see, there is a good lesson to show here.  Once can not assume that (2) similar functions or methods in the framework may not behave the same even if they are similar in purpose.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 2, 2009 2:47 PM