PDA

Click to See Complete Forum and Search --> : Classic VB - How do I put the " character into a string?


si_the_geek
Jul 18th, 2005, 05:31 PM
The " character is used to start or end a string, so the following fails:
Dim myString as String
myString = "I want a quote in my string " right there!"

This is because VB thinks that you want the string to end before the word right.


To put the quote character into a string you need to use one of the following methods:

Method 1: Use twice as many quote characters as you want, eg:
Dim myString as String
myString = "I want a quote in my string "" right there!"


Method 2: Use a constant to represent the quote character, and add that to your string. This method is probably the easiest to read:
Const QUOTE = """"
Dim myString as String
myString = "I want a quote in my string " & QUOTE & " right there!"Note that the "Const ... " line can be placed in the General Declarations section of a form/module for use throughout.


Method 3: Use the Chr function (which returns the character denoted by the ascii code you provide) with the ascii code of the " character (which is 34), eg:
Dim myString as String
' Chr(34) is the same as the " character
myString = "I want a quote in my string " & Chr(34) & " right there!"
Note that as this method involves a function it is slower than methods 1 and 2, especially in loops etc.