One of LiveCode’s strongest features is how it lets you work with and manipulate text.
When working with text you will usually first place it inside a container (also known as assigning a value to a variable) and then work with / manipulate the container.
You can assign text to a variable in a number of ways, such as:
- Loading a text file or a url
- Working with an api
- Input from a form
- Retrieving data frim a database
LiveCode also has a number of constants (think of them as variables with a preset value, that you cannot alter. Constants make it easier to refer to certain characteres and values. LiveCodes constants include:
- space – blank space (i.e. between words)
- quote – gives a quotation mark ( ” )
- return – carriage return
- tab – tab space
- comma – comma ( , )
- a longer list of LiveCode’s server constants
A literal is that has the value of itself. E.g. anything enclosed in double quotes in LiveCode is treated as a literal.
One of the terms you will see frequently used with LiveCode is “Chunks”. Chunks are definitly one of LiveCodes more interesting features. Put simply a chunk can be any character, line, word paragraph, or any part of a string / variable you choose.
Retrieving a chunk is done via chunk expressions such as:
put "The quick brown fox" into myText put word 2 of myText // outputs: quick put char 5 of myText // outputs: q put char 1 of word 3 of myText // outputs: b -- if myVar consisted of a number of lines (e.g. text from a text file, you can access different lines aswell put line 1 of myText //
You can use chunks to edit a text
put "The quick brown fox" into myText put "slow" into word 2 of myText put myText // outputs: The slow brown fox
Think of items as a list. Each element of the list is called and item. By default items are delimited by commas ( , ) e.g:
put "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" into myList put item 5 of myList // outputs: Friday
You can change the delimiter by setting the itemDelimeter:
set the itemDelimiter to "/" put "Monday/Tuesday/Wednesday/Thursday/Friday/Saturday/Sunday" into myList put item 3 of myList // outputs: Wednesday
Working with Strings
In LiveCode a string is a group of characters. A string can be a single character, a paragraph of copy, a number etc. LiveCode does not differentiate between strings and integers. It will treat a string (or even part of string as integer when you request it.
Strings are asigned to a variable like so:
put "The cat in the hat" into myString put "456" into myNumber //etc
You can also use before or after to assign place the string at the beginning or the end of a variable:
put "He" into myVar put "llo" after myVar put myVar // outputs Hello put "llo" into myVar put "He" before myVar put myVar // outputs Hello
You can join chuncks of text by using the ampersand.
- & joins them together
- && joins 2 chunks together with a space seperating the 2 chunks
put "hello" & "world" // outputs: helloworld put "hello" && "world" // outputs: hello world
- You can also join chunks using a comma ( , ) – usefull when wanting to create a comma seperated list.
put "cats" , "dogs" , "birds" // outputs: cats,dogs,birds
Find and Replace
One of the most common operations performed on text is finding and replacing parts of a string.
put replaceText("monday","mon","thurs") // outputs: thursday
put "The quick brown fox" into haystack put "brown" into needle if haystack contains needle then put "Found" // outputs: found
ReverseÂ aÂ string
put "!dlroW olleH" into myString repeat with i = length(myString) down to 1 put char i of myString after reversedString end repeat put reversedString //outputs Hello World!