How to manage text strings that contain quotes

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

How to manage text strings that contain quotes

Post by Clarkey » Mon Jun 14, 2010 6:50 pm

Hi folks, I'm new to Revolution and don't have a programming background, so please excuse the obvious newbie post (and this may well be the first of many!).

How do I work with text strings that contain double quotes? I want to use the urlencode function to prepare the data element for a POST command but the quotes in the data string are creating errors.

This must be a common problem but I'm obviously not searching on the correct term. So, any insights most appreciated.
Best,
Keith..

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to manage text strings that contain quotes

Post by Klaus » Mon Jun 14, 2010 7:29 pm

Hi Keith,

what errors do you get and what did you script so far that causes the error(s)?


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: How to manage text strings that contain quotes

Post by dunbarx » Mon Jun 14, 2010 8:59 pm

Perhaps you are asking how to add double quotes? Double quotes frame literals, and for that reason they are also a constant in Revolution, so if you say:

answer "xyz", you will get xyz.

But if you say answer quote & "xyz" & quote, you will get "xyz".

This is a way to embed quotes in a string, otherwise hard to get one's head around.

Or is that not the issue?

Craig Newman

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: How to manage text strings that contain quotes

Post by Clarkey » Mon Jun 14, 2010 9:03 pm

Hi Klaus,
Thanks for responding. The post message I need to send contains double quotes - here is the example code:

Code: Select all

POST https://na1.salesforce.com/services/Soap/u/10.0 HTTP/1.1 
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 
1.1.4322.573) 
Content-Type: text/xml; charset=utf-8 
SOAPAction: "" 
Content-Length: 510 
Expect: 100-continue 
Host: na1.salesforce.com 
  
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:urn="urn:partner.soap.sforce.com">
  <soapenv:Body>
    <urn:login>
      <urn:username>user@domain.com</urn:username>
      <urn:password>secret</urn:password>
    </urn:login>
  </soapenv:Body>
</soapenv:Envelope>
My script currently looks like this:

Code: Select all

local tSalesforceURL --Salesforce login server
local tSalesforceUsername, tSalesforcePassword, tSalesforceSecurityToken --User credentials
local tSalesforceLoginMessage, tSalesforceLoginResult --Login POST message and result

global gSalesforceSessionId --Session ID returned following successful login
global gSalesforceMetadataServerUrl --Server for calls after login

put null into gSalesforceSessionId, gSalesforceMetadataServerUrl, tSalesforceLoginResult

on mouseUp
   ask "Enter your Salesforce Username"
   put it into tSalesforceUsername
   
   ask "Enter your Salesforce Password"
   put it into tSalesforcePassword
   
   ask "Enter your Salesforce Security Token"
   put it into tSalesforceSecurityToken
   
   put "http//www.salesforce.com" into tSalesforceURL
   
   put urlencode ("POST https://na1.salesforce.com/services/Soap/u/10.0 HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573) Content-Type: text/xml; charset=utf-8 Content-Length: 510 Expect: 100-continue Host: na1.salesforce.com <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:urn="urn:partner.soap.sforce.com"><soapenv:Body><urn:login><urn:username>tSalesforceUsername</urn:username><urn:password>tSalesforcePassword</urn:password></urn:login></soapenv:Body></soapenv:Envelope>) into tSalesforceLoginMessage
   
   post tSalesforceLoginMessage to URL "https://na1.salesforce.com/services/Soap/u/10.0"
   
   Post field "Return Values" to URL field "Current Page"
   
end mouseUp
So, urlencode (or even put) commands interpret first double quote as the end of the statement and I then get compilation errors that the function separators are not commas.
Best,
Keith..

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: How to manage text strings that contain quotes

Post by Clarkey » Mon Jun 14, 2010 9:06 pm

dunbarx wrote:Perhaps you are asking how to add double quotes? Double quotes frame literals, and for that reason they are also a constant in Revolution, so if you say:

answer "xyz", you will get xyz.

But if you say answer quote & "xyz" & quote, you will get "xyz".

This is a way to embed quotes in a string, otherwise hard to get one's head around.

Or is that not the issue?

Craig Newman
Craig, thanks for responding - as you can see, the issue is that I have double quotes residing within the literals. This is probably just Revolution naivety on my part as I'm trying to get my head around a whole new set of concepts here.
Best,
Keith..

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: How to manage text strings that contain quotes

Post by Curry » Mon Jun 14, 2010 10:39 pm

You can escape quotes thusly:

put "blahblah" & quote & "blahblah" into myVar

You can add variables into the mix that way too.

Or you could put your post data into a container such as custom property or field, and:

put urlencode (the postData of this stack) into tSalesforceLoginMessage
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: How to manage text strings that contain quotes

Post by Clarkey » Tue Jun 15, 2010 7:35 am

Thanks Curry, I slept on the problem and awoke thinking that maybe I should break the message payload content down into chunks to allow reuse and avoid the embedded quotes issue - and your first two lines hit the spot on how I might achieve this.

I can also see that the container approach has its merits though - placing the message content in off-card fields would help during development for maintenance. So, I think I'll end up with a hybrid of these.
Brilliant - thanks for the help.
Best,
Keith..

Post Reply