Page 1 of 1
Substring function
Posted: Wed Apr 14, 2010 7:15 pm
by jwbuzz
I'm trying to simply remove the extension from a filename that I have in a variable. I can't seem to find a substring type of function or keyword.
Ex:
put "My Song.mp3" into tTrack
answer tTrack // I want this to print "My Song" without the extension.
Re: Substring function
Posted: Wed Apr 14, 2010 7:53 pm
by Klaus
Hi jwbuzz,
this is a case for the very flexible and settable "itemdelimiter"!
...
put "My Song.mp3" into tTrack
set itemdel to "."
## Display all ITEMS but the last of tTrack
answer item 1 to -2 of tTrack
...
Cool, isn't it?
You could also e.g.:
delete char -4 to -1 of tTrack
Hope that helps!
Best from germany
Klaus
Re: Substring function
Posted: Wed Apr 14, 2010 8:53 pm
by jwbuzz
Thanks.. That's great.. Just a little unfamiliar coming from other languages.
Re: Substring function
Posted: Wed Apr 14, 2010 11:09 pm
by Zryip TheSlug
You could also resolved by thinking like this : I would remove the extension of my file.
So:
Code: Select all
put "My Song.mp3" into tTrack
set itemdel to "."
delete last item of tTrack
return tTrack
Now for manipulating substring, you can play with:
-
items introduced nicely by Klaus
-
chars
e.g. :
-> return "Gre"
-
words
e.g. :
Code: Select all
put word 2 to 3 of "That is great"
-> return "is great"
-
lines
e.g. :
Code: Select all
put line 1 to 2 of "That is great
really great
Cool!"
-> return "That is great
really great"
and you can combining all this key words:
e.g.:
Code: Select all
put char 1 to 2 of word 2 of line 2 of "That is great
really great
Cool!"
-> return "gr"
There is language and language, depending that if you think like computers or humans
Regards,