Announcement

Collapse
No announcement yet.

How to construct a string with embedded double quotes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to construct a string with embedded double quotes

    It's been a while since I used PB. Trying to construct a string something like "hello "Bob" and Mary" and I have been unable to do it. Docs say use 2 quotes like "Hello ""Bob"" and Mary" but I keep getting syntax error. Thanks in advance for any help.

  • #2
    "Hello " + $DQ + "Bob" + $DQ + " and Mary"

    Comment


    • #3
      Howdy, Ray!

      No error with this ...

      Code:
      Function PBMain() As Long
         ? "Bob and ""Mary"" are friends!"
      End Function

      Comment


      • #4
        Originally posted by Rod Macia View Post
        "Hello " + $DQ + "Bob" + $DQ + " and Mary"
        Hi Rod. Tried this way. Gives "Hello ""Bob"" and Mary". [double quotes arond Bob]. Not what I need. Thanks

        Comment


        • #5
          Hello Gary! Long time since I've been on this board. Your code displays correctly. I'm going to play with it to see if I can get it into a string.

          Comment


          • #6
            PBWIN code:

            LOCAL Bob, _
            Mary AS STRING

            Bob$ = "Bob"
            Mary$ = "Mary"

            Bob$ = "Hello " + Bob$ + " and " + Mary$

            MSGBOX Bob$

            In PBCC you could do it like this:

            PRINT "Hello "; Bob$; " and "; Mary$
            Walt Decker

            Comment


            • #7
              Hello Walt. Going round & round with this thing.

              LOCAL Bob, _
              Mary,t AS STRING

              Bob$ = "Bob"
              Mary$ = "Mary"

              Bob$ = "Hello " + Bob$ + " and " + Mary$
              t = ""

              using t just for a breakpoint. Evaluating Bob$ gives "Hello Bob and Mary". Unfortunately still not what I need.

              Comment


              • #8
                Hi Ray,
                Two ways:

                Code:
                   LOCAL sMyString AS STRING
                
                   sMyString = "hello ""Bob"" and Mary"
                   ? sMyString
                
                   sMyString = "hello " + $DQ + "Bob" + $DQ + " and Mary"
                   ? sMyString

                Output:
                Code:
                hello "Bob" and Mary
                hello "Bob" and Mary

                Comment


                • #9
                  Originally posted by Ray Crumrine View Post
                  Hello Walt. Going round & round with this thing.

                  LOCAL Bob, _
                  Mary,t AS STRING

                  Bob$ = "Bob"
                  Mary$ = "Mary"

                  Bob$ = "Hello " + Bob$ + " and " + Mary$
                  t = ""

                  using t just for a breakpoint. Evaluating Bob$ gives "Hello Bob and Mary". Unfortunately still not what I need.
                  Correct, Walt's example didn't use quotes at all.

                  There are lots of ways to do it. Here's four for a start without even looking at the STRINGBUILDER object

                  '
                  Code:
                  #COMPILE EXE
                  #DIM ALL
                  FUNCTION PBMAIN () AS LONG
                   LOCAL strTemp AS STRING
                   strTemp = "Hello ""Bob"" and Mary"
                   ? strTemp
                   strTemp = "Hello " & $DQ & "Bob" & $DQ & " and Mary"
                   ? strTemp
                    strTemp = "Hello " & CHR$(34) & "Bob" & CHR$(34) & " and Mary"
                   ? strTemp
                   strTemp = CHR$("Hello",$DQ,"Bob",$DQ,"and Mary")
                   ? strTemp
                  END FUNCTION
                  '

                  Comment


                  • #10
                    I was about to post. As long as all I do is display it to a message box using ? it works but if I try to put it into a string variable it evaluates to "Hello ""Bob"" and Mary" every time!
                    must be some esoteric way of doing it.

                    Comment


                    • #11
                      Ray,

                      In the examples that Stuart and I posted, we're using a string variable and the results look like what you're expecting.

                      Comment


                      • #12
                        Ray can you show the failing code you are using?

                        is it possible you are using a
                        Opening double quote
                        Closing double quote

                        instead of
                        " Straight double quote

                        Comment


                        • #13
                          Originally posted by Rod Macia View Post
                          Ray can you show the failing code you are using?

                          is it possible you are using a
                          Opening double quote
                          Closing double quote

                          instead of
                          " Straight double quote
                          Or multiple single quotes?

                          Comment


                          • #14
                            Hi Rod,

                            FUNCTION PBMAIN() AS LONG
                            LOCAL strTemp, t AS STRING
                            strTemp = "Hello ""Bob"" and Mary"
                            t=""
                            END FUNCTION

                            Put a breakpoint on the t="" line and evaluate strTemp. Evaluates to "Hello ""Bob"" and Mary" every time. I want to use the resultant string to feed a Windows/DOS command line and quotes around "Bob" are required. Maybe I'm missing something simple. I only see double quote on SHIFTed apostrophe key. I tried CHR$(34) to see if it made any difference but it didn't.

                            Comment


                            • #15
                              Maybe I'll try PEEKing through the string and see what I find... Maybe an issue with the debugger but I'm pretty sure I tried sending the string to DOS and keep getting "syntax error"

                              Comment


                              • #16
                                I think the issue here may be what is shown (evaluated) in the PBEditor Variable watcher dialog during a debug session - not the text that the program delivers say, to a Message box ?
                                Rgds, Dave

                                Comment


                                • #17
                                  Dave is correct nothing wrong with the content of your variable its the way the debugger displays the content that is confusing.

                                  Click image for larger version

Name:	ray.PNG
Views:	63
Size:	14.8 KB
ID:	798510

                                  Comment


                                  • #18
                                    Originally posted by Ray Crumrine View Post
                                    Hi Rod,

                                    FUNCTION PBMAIN() AS LONG
                                    LOCAL strTemp, t AS STRING
                                    strTemp = "Hello ""Bob"" and Mary"
                                    t=""
                                    END FUNCTION

                                    Put a breakpoint on the t="" line and evaluate strTemp. Evaluates to "Hello ""Bob"" and Mary" every time. I want to use the resultant string to feed a Windows/DOS command line and quotes around "Bob" are required. Maybe I'm missing something simple. I only see double quote on SHIFTed apostrophe key. I tried CHR$(34) to see if it made any difference but it didn't.
                                    I see, It's all a misunderstanding on your part.
                                    You are looking at a Watch on strTemp while debugging, not actually displaying the string

                                    Click image for larger version  Name:	dq.jpg Views:	1 Size:	9.6 KB ID:	798512

                                    That is NOT the actual content of the string. Note that it also shows a leading and trailing double quote on the string. That Value is "escaping" the embedded double quote as well as surrounding the variable with the double quotes

                                    Try
                                    ? str$(len(strTemp)) It is 20, not 24
                                    or
                                    ? Mid$(strTemp, 6, 9).

                                    Comment


                                    • #19
                                      Originally posted by Ray Crumrine View Post
                                      Maybe I'll try PEEKing through the string and see what I find... Maybe an issue with the debugger but I'm pretty sure I tried sending the string to DOS and keep getting "syntax error"
                                      What do you mean by "sending the string to DOS" ?
                                      Please post the line generating the "syntax error" and details of the error.

                                      Comment


                                      • #20
                                        Code:
                                        FUNCTION PBMAIN() AS LONG
                                        LOCAL strTemp, t AS STRING
                                        strTemp = "Hello ""Bob"" and Mary"
                                        t=""
                                        
                                        ? strTemp + $CRLF + TxtToHex(strTemp)
                                        END FUNCTION
                                        
                                        
                                        FUNCTION TxtToHex (StrConvert AS STRING) AS STRING
                                        LOCAL i AS LONG
                                        LOCAL sb AS ISTRINGBUILDERA
                                        sb = CLASS "StringBuilderA"
                                        
                                        FOR i=1 TO LEN(StrConvert)
                                        sb.add HEX$(ASC(StrConvert,i),2)+ $SPC
                                        NEXT
                                        FUNCTION = sb.string
                                        END FUNCTION ' TxtToHex
                                        Code:
                                        ---------------------------
                                        PowerBASIC
                                        ---------------------------
                                        Hello "Bob" and Mary
                                        
                                        48 65 6C 6C 6F 20 22 42 6F 62 22 20 61 6E 64 20 4D 61 72 79
                                        ---------------------------
                                        OK
                                        ---------------------------

                                        Comment

                                        Working...
                                        X