Working With Text
If you are writing an application that works with text, whether it is entered by the user or read in from 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
allow 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.
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 'Cum sociis natoque penatibus et..."
Note: These examples and comments relate to the text shown in the first example of this lesson.
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:
set the itemDelimiter to ":"
answer item 7 of field "text"
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
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".
find "urna" in field "text"
The replace Command
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 "N" with "NNN":
put the text of field "text" into tFieldText
put replaceText(tFieldText, "N[A-Za-z]*","NNN") into field "text"
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.