|
-
Nov 20th, 2002, 04:48 PM
#1
Thread Starter
Addicted Member
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.
-
Nov 20th, 2002, 06:27 PM
#2
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!!
-
Nov 21st, 2002, 06:06 AM
#3
Thread Starter
Addicted Member
Thanks that works just fine!
Wind and waves resolves all problems.
-
Nov 21st, 2002, 08:34 AM
#4
Hyperactive Member
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.
-
Nov 21st, 2002, 03:21 PM
#5
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!!
-
Nov 21st, 2002, 03:49 PM
#6
Hyperactive Member
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:
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:
VB Code:
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.
-
Nov 21st, 2002, 04:02 PM
#7
-
Nov 21st, 2002, 04:16 PM
#8
Hyperactive Member
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:
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.
-
Nov 21st, 2002, 11:05 PM
#9
Lively Member
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?
-
Nov 22nd, 2002, 12:43 AM
#10
PowerPoster
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.
-
Nov 22nd, 2002, 12:53 AM
#11
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|