How do I remove certain data from a variable. Say that the variable “var1” is “12345”. How would I remove the number “3” from that variable so the end result would read “1245”?
Access the variable’s component texts and piece them back together (concatenation).
set var1 to 12345
tell (var1 as string)'s text to set var1 to its strings 1 thru 2 & strings -2 thru -1 as number
A few weeks ago I was trying to delete one item in a list and was coming up short.
This works, but seems inelegant:
set var1 to the characters of "The quick brown fox"
tell var1 to set var1 to its items 1 thru 6 & items 8 thru -1
Is there an alternative?
Here’s two possible methods that allow for a little more flexibility.
set tid to AppleScript's text item delimiters
set a to "12345"
set AppleScript's text item delimiters to "3"
set b to (every text item of a)
set AppleScript's text item delimiters to tid -- DONT FORGET THIS LINE or you'll have to restart Script Editor.
-- If you do the value you use will persist until Script Editor is restarted.
b as Unicode text
set a to "12345"
set searchValue to "23"
set characterOffset to (offset of searchValue in a)
set b to (characters 1 through (characterOffset - 1) of a) & (characters (characterOffset + (count searchValue)) through (-1) of a) as Unicode text
Both methods allow you to find a value without having to know where in the string it is. They both also allow for a varying number of charatcers in your search string.
Have fun!
Jim Neumann
BLUEFROG