Testing for double apostrophe in a string

Discussion in 'ASP General' started by Les Juby, May 12, 2005.

  1. Les Juby

    Les Juby Guest

    A client needs a routine to alert him as to which memo records in an
    Access-2000 database have had double apostrophes inserted in the text.
    These are stopping a Java mouseover from executing.

    Normally, while cycling through the recordset, I would use the InStr
    function to test for a positive result indicating and embedded text.

    ie. If InStr(rstemp("Keywords"),"searched") > 0 Then

    would give a true for all <keywords> fields that contained the text
    < searched >

    But what do you do if you're looking for a double apostrophe itself.?

    To specify the string you are searching for you can't use a single
    apostrophe to deliniate the string, such as ' " ' and of course
    using " " " won't work either.

    Is there a character expression maybe that one could use instead.?

    Or can someone perhaps suggest shattering my paradigm by using
    something other than an If test on a function. Or maybe a different
    function.?

    thanks ( in anticipation )

    . les .
    Les Juby, May 12, 2005
    #1
    1. Advertising

  2. How about passing it through a function to replace the ' with \'?

    <%
    sValue = "I'm a string"
    Function jsEncode(s)
    jsEncode = Replace(s, "'", "\'")
    End Function
    %>

    <button onclick="alert('<%=jsEncode(sValue)%>');">Click me</button>


    Ray at work





    "Les Juby" <> wrote in message
    news:...
    > A client needs a routine to alert him as to which memo records in an
    > Access-2000 database have had double apostrophes inserted in the text.
    > These are stopping a Java mouseover from executing.
    >
    > Normally, while cycling through the recordset, I would use the InStr
    > function to test for a positive result indicating and embedded text.
    >
    > ie. If InStr(rstemp("Keywords"),"searched") > 0 Then
    >
    > would give a true for all <keywords> fields that contained the text
    > < searched >
    >
    > But what do you do if you're looking for a double apostrophe itself.?
    >
    > To specify the string you are searching for you can't use a single
    > apostrophe to deliniate the string, such as ' " ' and of course
    > using " " " won't work either.
    >
    > Is there a character expression maybe that one could use instead.?
    >
    > Or can someone perhaps suggest shattering my paradigm by using
    > something other than an If test on a function. Or maybe a different
    > function.?
    >
    > thanks ( in anticipation )
    >
    > . les .
    >
    Ray Costanzo [MVP], May 12, 2005
    #2
    1. Advertising

  3. here is a function I wrote a couple years back.
    (probably can be simplified/improved but works fine)

    I use the javascript part of it just for the purpose you are talking about

    Function FixStr(StrToFix,FixHow)
    ' This function prepares a string for saving in the database
    ' And also can prepare a string for editing in a text box
    ' The reason we need to do this is to stop users from entering HTML or
    Javascript
    ' And also to make sure line breaks get converted to <br> tags

    If Not IsNull(StrToFix) and StrToFix <> "" Then

    If FixHow = "PrepareForSave" then
    StrToFix = replace(StrToFix,"<","&lt;")
    StrToFix = replace(StrToFix,">","&gt;")
    StrToFix = Replace(StrToFix,vbCr,"<br>")
    ElseIf FixHow = "PrepareForTextBox" then
    StrToFix = replace(StrToFix,"&lt;","<")
    StrToFix = replace(StrToFix,"&gt;",">")
    StrToFix = Replace(StrToFix,"<br>",vbCr)
    ElseIf FixHow = "PrepareForJavaScript" then
    StrToFix = replace(StrToFix,"'","\'")
    StrToFix = replace(StrToFix,"""","\'\'")
    StrToFix = Replace(StrToFix,vbCrLf,"<br>")
    StrToFix = Replace(StrToFix,vbLf,"<br>")
    StrToFix = Replace(StrToFix,vbNewLine,"<br>")
    End If

    End If

    FixStr = StrToFix
    End function


    and you use it like this

    FixStr(YourVariable,"PrepareForJavaScript")

    I got the idea from an article at
    www.powerasp.com




    "Les Juby" <> wrote in message
    news:...
    >A client needs a routine to alert him as to which memo records in an
    > Access-2000 database have had double apostrophes inserted in the text.
    > These are stopping a Java mouseover from executing.
    >
    > Normally, while cycling through the recordset, I would use the InStr
    > function to test for a positive result indicating and embedded text.
    >
    > ie. If InStr(rstemp("Keywords"),"searched") > 0 Then
    >
    > would give a true for all <keywords> fields that contained the text
    > < searched >
    >
    > But what do you do if you're looking for a double apostrophe itself.?
    >
    > To specify the string you are searching for you can't use a single
    > apostrophe to deliniate the string, such as ' " ' and of course
    > using " " " won't work either.
    >
    > Is there a character expression maybe that one could use instead.?
    >
    > Or can someone perhaps suggest shattering my paradigm by using
    > something other than an If test on a function. Or maybe a different
    > function.?
    >
    > thanks ( in anticipation )
    >
    > . les .
    >
    Kyle Peterson, May 12, 2005
    #3
  4. you have to test for those line breaks too and convert them to "<br>"'s
    because if their are any it messes up your javascript as well

    probably overkill the way I am testing for line breaks but like I said it
    works well under all scenarios I have come up with so far

    so like Grandma used to say "**** it !"


    "Kyle Peterson" <> wrote in message
    news:%...
    > here is a function I wrote a couple years back.
    > (probably can be simplified/improved but works fine)
    >
    > I use the javascript part of it just for the purpose you are talking about
    >
    > Function FixStr(StrToFix,FixHow)
    > ' This function prepares a string for saving in the database
    > ' And also can prepare a string for editing in a text box
    > ' The reason we need to do this is to stop users from entering HTML or
    > Javascript
    > ' And also to make sure line breaks get converted to <br> tags
    >
    > If Not IsNull(StrToFix) and StrToFix <> "" Then
    >
    > If FixHow = "PrepareForSave" then
    > StrToFix = replace(StrToFix,"<","&lt;")
    > StrToFix = replace(StrToFix,">","&gt;")
    > StrToFix = Replace(StrToFix,vbCr,"<br>")
    > ElseIf FixHow = "PrepareForTextBox" then
    > StrToFix = replace(StrToFix,"&lt;","<")
    > StrToFix = replace(StrToFix,"&gt;",">")
    > StrToFix = Replace(StrToFix,"<br>",vbCr)
    > ElseIf FixHow = "PrepareForJavaScript" then
    > StrToFix = replace(StrToFix,"'","\'")
    > StrToFix = replace(StrToFix,"""","\'\'")
    > StrToFix = Replace(StrToFix,vbCrLf,"<br>")
    > StrToFix = Replace(StrToFix,vbLf,"<br>")
    > StrToFix = Replace(StrToFix,vbNewLine,"<br>")
    > End If
    >
    > End If
    >
    > FixStr = StrToFix
    > End function
    >
    >
    > and you use it like this
    >
    > FixStr(YourVariable,"PrepareForJavaScript")
    >
    > I got the idea from an article at
    > www.powerasp.com
    >
    >
    >
    >
    > "Les Juby" <> wrote in message
    > news:...
    >>A client needs a routine to alert him as to which memo records in an
    >> Access-2000 database have had double apostrophes inserted in the text.
    >> These are stopping a Java mouseover from executing.
    >>
    >> Normally, while cycling through the recordset, I would use the InStr
    >> function to test for a positive result indicating and embedded text.
    >>
    >> ie. If InStr(rstemp("Keywords"),"searched") > 0 Then
    >>
    >> would give a true for all <keywords> fields that contained the text
    >> < searched >
    >>
    >> But what do you do if you're looking for a double apostrophe itself.?
    >>
    >> To specify the string you are searching for you can't use a single
    >> apostrophe to deliniate the string, such as ' " ' and of course
    >> using " " " won't work either.
    >>
    >> Is there a character expression maybe that one could use instead.?
    >>
    >> Or can someone perhaps suggest shattering my paradigm by using
    >> something other than an If test on a function. Or maybe a different
    >> function.?
    >>
    >> thanks ( in anticipation )
    >>
    >> . les .
    >>

    >
    >
    Kyle Peterson, May 12, 2005
    #4
  5. you have to test for those line breaks too and convert them to "<br>"'s
    because if their are any it messes up your javascript as well

    probably overkill the way I am testing for line breaks but like I said it
    works well under all scenarios I have come up with so far

    so like Grandma used to say "F@#! it !"


    "Kyle Peterson" <> wrote in message
    news:%...
    > here is a function I wrote a couple years back.
    > (probably can be simplified/improved but works fine)
    >
    > I use the javascript part of it just for the purpose you are talking about
    >
    > Function FixStr(StrToFix,FixHow)
    > ' This function prepares a string for saving in the database
    > ' And also can prepare a string for editing in a text box
    > ' The reason we need to do this is to stop users from entering HTML or
    > Javascript
    > ' And also to make sure line breaks get converted to <br> tags
    >
    > If Not IsNull(StrToFix) and StrToFix <> "" Then
    >
    > If FixHow = "PrepareForSave" then
    > StrToFix = replace(StrToFix,"<","&lt;")
    > StrToFix = replace(StrToFix,">","&gt;")
    > StrToFix = Replace(StrToFix,vbCr,"<br>")
    > ElseIf FixHow = "PrepareForTextBox" then
    > StrToFix = replace(StrToFix,"&lt;","<")
    > StrToFix = replace(StrToFix,"&gt;",">")
    > StrToFix = Replace(StrToFix,"<br>",vbCr)
    > ElseIf FixHow = "PrepareForJavaScript" then
    > StrToFix = replace(StrToFix,"'","\'")
    > StrToFix = replace(StrToFix,"""","\'\'")
    > StrToFix = Replace(StrToFix,vbCrLf,"<br>")
    > StrToFix = Replace(StrToFix,vbLf,"<br>")
    > StrToFix = Replace(StrToFix,vbNewLine,"<br>")
    > End If
    >
    > End If
    >
    > FixStr = StrToFix
    > End function
    >
    >
    > and you use it like this
    >
    > FixStr(YourVariable,"PrepareForJavaScript")
    >
    > I got the idea from an article at
    > www.powerasp.com
    >
    >
    >
    >
    > "Les Juby" <> wrote in message
    > news:...
    >>A client needs a routine to alert him as to which memo records in an
    >> Access-2000 database have had double apostrophes inserted in the text.
    >> These are stopping a Java mouseover from executing.
    >>
    >> Normally, while cycling through the recordset, I would use the InStr
    >> function to test for a positive result indicating and embedded text.
    >>
    >> ie. If InStr(rstemp("Keywords"),"searched") > 0 Then
    >>
    >> would give a true for all <keywords> fields that contained the text
    >> < searched >
    >>
    >> But what do you do if you're looking for a double apostrophe itself.?
    >>
    >> To specify the string you are searching for you can't use a single
    >> apostrophe to deliniate the string, such as ' " ' and of course
    >> using " " " won't work either.
    >>
    >> Is there a character expression maybe that one could use instead.?
    >>
    >> Or can someone perhaps suggest shattering my paradigm by using
    >> something other than an If test on a function. Or maybe a different
    >> function.?
    >>
    >> thanks ( in anticipation )
    >>
    >> . les .
    >>

    >
    >
    Kyle Peterson, May 12, 2005
    #5
  6. "Les Juby" <> wrote in message
    news:...
    >A client needs a routine to alert him as to which memo records in an
    > Access-2000 database have had double apostrophes inserted in the text.
    > These are stopping a Java mouseover from executing.
    >
    > Normally, while cycling through the recordset, I would use the InStr
    > function to test for a positive result indicating and embedded text.
    >
    > ie. If InStr(rstemp("Keywords"),"searched") > 0 Then
    >
    > would give a true for all <keywords> fields that contained the text
    > < searched >
    >
    > But what do you do if you're looking for a double apostrophe itself.?
    >
    > To specify the string you are searching for you can't use a single
    > apostrophe to deliniate the string, such as ' " ' and of course
    > using " " " won't work either.
    >
    > Is there a character expression maybe that one could use instead.?
    >
    > Or can someone perhaps suggest shattering my paradigm by using
    > something other than an If test on a function. Or maybe a different
    > function.?
    >
    > thanks ( in anticipation )
    >
    > . les .
    >


    If InStr(rstemp("Keywords"),Chr(34)) > 0 Then ...

    OR

    If InStr(rstemp("Keywords"),"""") > 0 Then ...

    Here's an article that discusses embedded quotes:
    http://aspfaq.com/show.asp?id=2065
    Chris Hohmann, May 12, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul reed
    Replies:
    0
    Views:
    424
    paul reed
    Oct 17, 2003
  2. =?Utf-8?B?d3J5dGF0?=

    Double Apostrophe "

    =?Utf-8?B?d3J5dGF0?=, May 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,607
    =?Utf-8?B?RWx0b24gVw==?=
    May 18, 2005
  3. Sydex
    Replies:
    12
    Views:
    6,475
    Victor Bazarov
    Feb 17, 2005
  4. Huy

    apostrophe or double quote?

    Huy, Feb 8, 2006, in forum: Python
    Replies:
    9
    Views:
    574
    Terry Hancock
    Feb 10, 2006
  5. MRW
    Replies:
    1
    Views:
    566
Loading...

Share This Page