Results 1 to 1 of 1

Thread: Classic VB - How do I put the " character into a string?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Classic VB - How do I put the " character into a string?

    The " character is used to start or end a string, so the following fails:
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    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.
    Last edited by si_the_geek; Nov 24th, 2010 at 09:37 AM. Reason: fixed issue with code tags

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