PDA

Click to See Complete Forum and Search --> : Replace Method


cim3
Nov 20th, 2002, 03:48 PM
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?

MrPolite
Nov 20th, 2002, 05:27 PM
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 :D

cim3
Nov 21st, 2002, 05:06 AM
Thanks that works just fine!

pvb
Nov 21st, 2002, 07:34 AM
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.

MrPolite
Nov 21st, 2002, 02:21 PM
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?

pvb
Nov 21st, 2002, 02:49 PM
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:

sString = "O'Brien"
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:

sString = "O'Brien"
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.

MrPolite
Nov 21st, 2002, 03:02 PM
kinda makes sense, interesting.:D 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! :confused:

pvb
Nov 21st, 2002, 03:16 PM
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.

Dim str as string
str = "hello"
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.

nightshift
Nov 21st, 2002, 10:05 PM
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?

hellswraith
Nov 21st, 2002, 11:43 PM
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.

nightshift
Nov 21st, 2002, 11:53 PM
oh okies, thats cool then... had me worried... lol