Results 1 to 11 of 11

Thread: Replace Method

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181

    Replace Method

    I am currently trying to replace single quotes with double quotes to insert into a Database. However it ignores the following
    sString = "O'Brien"
    sString.Replace("'","''")
    Even if i use convert.tochar method it doesn't recognise ' in the string.
    Any one any ideas?
    Wind and waves resolves all problems.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Replace Method

    Originally posted by cim3
    I am currently trying to replace single quotes with double quotes to insert into a Database. However it ignores the following
    sString = "O'Brien"
    sString.Replace("'","''")
    Even if i use convert.tochar method it doesn't recognise ' in the string.
    Any one any ideas?
    I'm not 100% sure, but a common mistake that people make is that they think a certain function gets the arguments as ByRef, while it doesnt. If I'm not wrong, the replace function gets a ByVal variable and returns the changed value.
    see if this would work:
    sString = sString.Replace("'","''")


    I might be wrong though
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181

    Smile

    Thanks that works just fine!
    Wind and waves resolves all problems.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    It has nothing to do with ByVal args versus ByRef args. In .NET, strings are immutable, meaning that once a string is set it can never change. So all of the methods that appear available on the string type will always return a new version of your string.

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by pvb
    It has nothing to do with ByVal args versus ByRef args. In .NET, strings are immutable, meaning that once a string is set it can never change. So all of the methods that appear available on the string type will always return a new version of your string.
    eeeeh, are you saying that you can't get a string as ByRef and modify it?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    No, I'm saying that you can't modify a string once it's set, no matter what you do. Passing args ByRef or ByVal is a completely separate topic, regarding your first post, the answer is, no matter what, in .NET, a string once set can never be changed and has nothing to do with ByVal or ByRef. Make sense? lemme see if i can give an example from your code:
    VB Code:
    1. sString = "O'Brien"
    2. sString.Replace("'","''")
    The second line doesn't work, mostly because that's just not how it's designed to work. All of the methods, well most anyway, of the string type return values. (and just as a reminder, I'm explaining something that has nothing to do with ByVal versus ByRef, forget those for now). So if want to modify a string in .NET, you have to create a new string and assign the new string to a variable. That's done with the following:
    VB Code:
    1. sString = "O'Brien"
    2. sString = sString.Replace("'","''")
    The second line in above code creates a new string on the right-hand side and assigns it to the variable on the left-hand side. So sString has a new string assigned to it, not a modified version of it's original string. This can be confusing because the same variable is used on both sides. This is why string concatenation is sooooo slow, a new string must be created every time you add two strings together.

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    kinda makes sense, interesting. so:

    Dim str as string
    str = "hello"
    str = "world"



    that will create two strings, right?
    ahh it doesnt make sense, this way working with strings in .net should be really slow!
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  8. #8
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well yes, working with strings in terms of doing a large number of operations on them will be slow, that's why in dot net we've got the speed demon StringBuilder Class that is almost oddly faster that fast. Use that guy if you're gonna do tons of string operations. If you're just gonna perform a one or just a few operations on a string then just a plane ol string method is good enough, you won't notice the performance hit until the number of operations gets up there.
    VB Code:
    1. Dim str as string
    2. str = "hello"
    3. str = "world"
    The above code is a little different than the other examples. This code says on the second line, assign the value of "hello" to my string variable str. The third line says, assign the value of "world" to my variable str. What's important to note is that the space in memory that holds "hello" is not modified to hold "world", what happens is that a new space is created to hold "world" and that space assigned to the variable str.

  9. #9
    Lively Member
    Join Date
    Nov 2002
    Location
    Perth - Australia
    Posts
    105
    Originally posted by pvb
    What's important to note is that the space in memory that holds "hello" is not modified to hold "world", what happens is that a new space is created to hold "world" and that space assigned to the variable str.
    so "hello" is still residing in memory?

    how would you clean that up? by using str.replace?

    hmmm... thats kinda silly... redundant data stored in memory

    or do i have it arse-about?

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    No, the memory holding hello is released, then a new memory block is created for world, then the pointer to that memory is returned to the variable.

  11. #11
    Lively Member
    Join Date
    Nov 2002
    Location
    Perth - Australia
    Posts
    105
    oh okies, thats cool then... had me worried... lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width