Let's say you need to put quotes inside a string. The most common way I've seen developers do this is with Chr(34):
dim myString as string = "Hello " + chr(34) + "World" + chr(34)
Some developers choose to define a constant and use that instead:
dim myString as string = "Hello " + kQuote + "World" + kQuote
But did you know you can insert a quote into your string by using two quotes instead?
dim myString as string = "Hello ""World"""
Makes the string a little more legible, doesn't it?
6 comments:
Thanks for the tip!
I read somewhere that you can get into trouble with SQLlite if you use double quotes on SQL statements. Is it better to use:?
dim name as String
name="Marcos"
sql="Select * from mytable where first_name='"+name+"'"
just a thought.
"Makes the string a little more legible, doesn't it?"
No, it doesn't, IMO. There's a reason some of use use a constant.
But, to each his own.
Of course, but most people I've seen using constants or chr(34) simply didn't realize typing the quote twice was an option.
Will a backslash work as well, like in Perl?
e.g. "Hello \"World\""
It is great for a small example like you posted but I find if I am concatenating a large string with a lot of quotes syntax errors are much easier to figure out with a constant.
I use both depending on complexity. Good for folks to know they have the option :^)
Post a Comment