Removing characters from TextField

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.


I use

Dim s As String = ReplaceAll(textfield1.text, “’”, “’’”)

Then use s for your script.

I use this to shell-encode stuff, and it seems to work just fine:

s = s.ReplaceAll( "'", "'\\''" )
s = "'" + s + "'"

So the string barry's bar becomes 'barry'\\''s bar'

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.


There is no “Contains” statement.

if TextField1.Text.InStr( "'" ) <> 0 then 

Thanks Kem, that looks good for single quotes!

However, it doesn’t seem to work with double quotes.

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.

Awesome! Thanks!

I’m glad you got it, but my second example had a typo (and I can’t edit it now). It should have read: "I mean ""this and that"""

That’s OK. I used

[code] if TextField1.Text.InStr( “”"" ) <> 0 then

MsgBox "Please don't use contractions or quotes!"



and it worked great!

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.

I have no idea how to tie in what you’ve written here to the string I’m getting from the textfield.

And I’m passing all this down to an AppleScript, which among other things is doing the shell script.

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.

Any ideas?

Shouldn’t that be

s = s.ReplaceAll( "'", "\\'" )


That works with single quotes or apostrophes!

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.

So…thanks Tim!

No, for the shell you want to close the quote, then escape it, then open the new quote.

Can you post the AppleScript code? Maybe you don’t need it at all.

Well, I know it doesn’t work, so I need it I guess.

So can you post that code?