Skip to main content
April 28, 2009
Question

Dealing with strings that have single and double quotes embedded

  • April 28, 2009
  • 1 reply
  • 4904 views

I've searched for this but I can't find the answer.  I have a string that looks like this:

The quick \red\ "fox" jumped over the 'lazy' brown dog.

Of course, when I set that as a string, Coldfusion can't handle it because it has both single and double quotes embedded in it.  I'm sure there's a simple way to strip the quotes (single or double) out of it, but I can't figure it out.

Any help would be greatly appreciated.

T

    This topic has been closed for replies.

    1 reply

    ilssac
    Inspiring
    April 29, 2009

    Strip or escape?  What exactly are you trying to do?

    Normally I would sugest that ColdFusion uses doubling to handle control characters so you can set that string like this.

    The quick \red\ ""fox"" jumped over the ''lazy'' brown dog.

    You would normally only need to double the type of quotes you are using to delcare the string since CF considers the single and double quote to be interchangable.

    I.E.

    <cfset this = "The quick \red\ ""fox"" jumped over the 'lazy' brown dog.">

    OR

    <cfset that = 'The quick \red\ "fox" jumped over the ''lazy'' brown dog.'

    If this does not help, we will need to know more about what you are really trying to do.

    April 29, 2009

    Sorry, I guess I don't write well.

    Ok, here's my problem.  I have no control over the incoming data - in some cases, the string as no quotes in it, but sometimes they do.  So the example string is sort of a worst case scenario.

    The quick \red\ "fox" jumped over the 'lazy' brown dog.

    Basically, I want to be able to set a variable to that string (something like):

    <cfset StringVariable="The quick \red\ "fox" jumped over the 'lazy' brown dog.">

    Of course, that won't work on this string, because the first " in front of fox ends the string, and if I use single quotes, the first ' in front of lazy ends the string.

    I'm assuming I need to escape the quotes, so I was thinking I could use HTMLEditFormat to do that, but it doesn't appear to work.

    T

    Inspiring
    May 20, 2009

    Where is the data coming from?

    IIRC if the string with quotes is already data then you can just set that data to a variable.

    How are you getting the data and how are you setting it to a variable that is causing it to truncate at the quotes.

    But if you need to modify the data, that is how you would modify it.  But if that is truly your situation I see this as a bit of a chicken and egg question.  In order to modify the string it needs to be data but to get to be data you need to modify the string.


    I have exactly the same problem so I'm going to add what I've determined to date.

    First, there only appears to be a problem when you're dealing with text boxes.  If you working with data in a text area there is no problem.  The single or double quotes go into the database and come back out into the text area just fine.  In text areas you put the data back into the page without having to use either single or double quotes: e.g., <textarea name="TB1" ... wrap="VIRTUAL">#sProbDesc#</textarea>.

    I think the problem relates to the way you have to put the info back into the text box.  Here is an example:

    <input name="TB1" type="text" id="TB1" value="#sTitle#" size="100" maxlength...>

    My value, "#sTitle#" is sourrounded by double quotes so you can have single quotes in the data and all is well.  If I turn it around and use '#sTitle#', then I can have double quotes in the data but not single quotes.

    The answer seems simple then, just leave off the quotes around the data in the text box and all will be fine.  That's not the answer either.  It seems to work differently depending upon the data.  A simple string apprears to terminate at the first blank so you get one word back into the field.  A complex sting can create chaos acrtoss on the whole page.  

    To me that fault lies with ColdFusion and the way it builds the page.  There does not seem to be a quote requirment for other things that normally come with double quotes around them like width="50", rows="2", etc.  You can leave the double quotes off of them and all works normally.  Why do you have to put either a single or double quote around the value="#sTitle#" to get a text box to work properly???  One would think that the required # # delimiters should be sufficient for ColdFusion to know where the string begins and ends.

    I know that you could escape every single or double quote before you insert it into a text box value field but that would be an unreasonble requirement.  I just let DW count the number of text boxes in my system: 166 in 716 files so expecting me to go to every one of these and escape the data is unreasonable, at best.

    If anyone knows a solution to this problem, I would greatly appreciate your sharing.

    Thanks in advance.

    :-}}}

    Len