How do I quickly and easily check for single and double quotes from a string generated my a textfield? The are reeking havoc when I pass them to a UNIX shell script.
Ideally, I’d like to be able to check for them (Having trouble with this part. Real is getting confused when I check for single quotes etc…) so I can notify the user that they aren’t allowed.
I’d also settle for advice on how to just strip them out.
Thanks Wayne, that helps somewhat. However, I’m going to be recording this text, and I’m not sure if everything will sound phonetically right by replacing one type of character with another. Ideally I’d like to check for apostrophes, and then warn the user not to use contractions.
When I use this code,
if TextField1.text contains ("'") then
Xojo complains about not having a “Then” behind the if, so clearly the quotes and single quotes are throwing off the code.
If I could get that working…well…that would be ideal.
To quote double-quotes, you have to do it like this: """". In a larger string, say I mean "this and that", it would look like this: I mean ""this and that""" For each individual quote, double it up.
Great. Now that that’s out of the way, I don’t think you should be limiting your user, you should be encoding the parameter before sending it to the shell in the way I described above. You can use this:
Function EncodeForShell (value as string) As String
value = value.ReplaceAll( "'", "'\\''" )
value = ReplaceLineEndings( value, EndOfLine.UNIX )
return "'" + value + "'"
End Function
If the input string is didn't I say, EncodeForShell will give you 'didn'\\''t I say' and that will work perfectly.
So, I think I’ll be fine with my warning about contractions. The user is not writing the great American novel…just typing in a few words. They can type "do not " instead of “don’t”.
All right, so I changed my mind and wanted to use your earlier code, Kem.
s = s.ReplaceAll( "'", "'\\''" )
s = "'" + s + "'"
This seems to work great when outputting to a msgbox. It’s formatting the text like you say it will. However, when I pass it down to the AppleScript, and it runs the shell script, it’s puking if I use any number of single quotes (apostrophes) or a single double quote. Two double quotes are the only combination that works correctly.
However, it’s still bombs on a single double quote. That being said, I can check for that and prompt the user not to use double quotes. I mean, there would be no reason to use double quotes…where as apostrophes and contractions are legit.