Substring function

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jwbuzz
Posts: 39
Joined: Tue Apr 13, 2010 7:56 pm

Substring function

Post by jwbuzz » Wed Apr 14, 2010 7:15 pm

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.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Substring function

Post by Klaus » Wed Apr 14, 2010 7:53 pm

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

jwbuzz
Posts: 39
Joined: Tue Apr 13, 2010 7:56 pm

Re: Substring function

Post by jwbuzz » Wed Apr 14, 2010 8:53 pm

Thanks.. That's great.. Just a little unfamiliar coming from other languages.

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Substring function

Post by Zryip TheSlug » Wed Apr 14, 2010 11:09 pm

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. :

Code: Select all

put char 1 to 3 of "Great!"
-> 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,
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

Post Reply