Working With Text
If you are writing an application that works with text, whether it is entered by the user or read in from a file, you will want to be able to process that text.
LiveCode has first-class text processing capabilities. LiveCode’s unique chunk expressions – the ability to refer to text using English-like statements like
word 3 to 5 of field 1
allows you to break up, parse and process text easily. In combination with other features including find and replace commands and regular expressions, text processing in LiveCode is simple and fast.
For this lesson I am using an example stack consisting of a field named 'text' and a button named 'try'. I will be editing the script of the button in each example.
Chunk expressions are the primary method of working with text in LiveCode. A chunk is an English-like way of describing an exact portion of text. You can use chunks both to retrieve a portion of text and to edit text.
The common types of chunks are character, word, line, and item. An item can be delimited by any character you specify.
Containers are sources of information that can be edited using chunk expressions. LiveCode has seven container types: variables, fields, buttons, images, URLs, the selection, and the message box.
You can use a chunk of a container anywhere you use an entire container. For example, you can use the add command to add a number to a line of a field.
Retrieving a Chunk
The simplest chunk expression specifies a single chunk of any type. The following statements are all valid chunk expressions:
get char 2 of field "text" -- returns 'o'
get word 4 of field "text" -- returns 'sit'
get line 3 of field "text" -- returns 'Cras eu tincidunt erat..."
Note: These examples and comments relate to the text shown in the first example of this lesson.
You can also use chunk expressions to edit text by putting a piece of text into a particular chunk. For example add this to the script of the "Try" button:
put "Hello world" into line 1 of field "text"
The first line of text in the field has been replaced with "Hello World".
By default, items are delimited by commas. You can change the default comma to create your own chunk type by setting the itemDelimiter property to any character. For example enter a list of months (1) separated by : in the text field, and put this in the Try button script (2):
on mouseUp set the itemDelimiter to ":" answer item 7 of field "text" end mouseUp
Switch to run mode, and click the button. You should see the dialog pop up with "July" in it (3).
An item can contain characters or words, but not lines. Items can be contained in a line, but not in a word or character.
Negative Indexes in Chunk Expressions
To count backwards from the end of the value instead of forward from the beginning, specify a negative number.
For example word -1 of field "text" specifies the last word of the field, word -2 the second last word and so on.
Complex Chunk Expressions
More complex chunk expressions can be constructed by specifying a chunk within another chunk. For example, the chunk expression word 4 of line 250 specifies the fourth word of line 250.
When combining chunks of different types to construct a complex chunk expression, you must specify the chunk types in order.
This statement is a valid chunk example:
char 4 of word 9 of line 3 of field "text"
but this is not
word 9 of char 3 of field "text"
Specifying a Range
To specify a portion larger than a single chunk, you specify the beginning and end of the range. Put this in the Try button:
answer char 2 to 4 of word 1 of field "text"
The find Command
The find command allows you to search for a piece of text in the fields of the current stack. You specify the text you want to find and, optionally, the field you want to search in.
When the search is successful a box is drawn around the found text. If the text is not found in the stack, the result function returns "Not found". Put this in your Try button:
on mouseUp find "urna" in field "text" end mouseUp
The replace Command
The replace command can be used to replace all instance of one string with another string, You specify the oldString, newString and container in which the text is to be replaced. Put this in the Try button script:
on mouseUp replace "at" with "XX" in field "text" end mouseUp
The replaceText Function
The replaceText function uses a regular expression to search for and replace text in a string. You specify the stringToChange, matchExpression and replacementString.
This example replaces all the words that begin with a capital "N" with "NNN":
on mouseUp put the text of field "text" into tFieldText put replaceText(tFieldText, "N[A-Za-z]*","NNN") into field "text" end mouseUp
Note: Because replaceText takes a string we put the contents of the field into a variable, use the replaceText function on the variable and put the result back into the field.