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.
Announcement
Collapse
No announcement yet.
How to construct a string with embedded double quotes
Collapse
X
-
Originally posted by Ray Crumrine View PostHello 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.
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
-
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
-
Originally posted by Ray Crumrine View PostHi 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.
You are looking at a Watch on strTemp while debugging, not actually displaying the string
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
-
Originally posted by Ray Crumrine View PostMaybe 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"
Please post the line generating the "syntax error" and details of the error.
Comment
-
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
Comment