Livecode Wiki

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 using itemdel.

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

Editing Text[]

You can also use chunk expressions to edit text by putting a piece of text into a particular chunk. For example:

put "Hello world" into line 1 of field "text"

The first line of text in the field has been replaced with "Hello World".

Item Example[]

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:

on mouseUp
 set the itemDelimiter to ":"
 answer item 7 of field "text"
end mouseUp

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

put word -1 of field "text" 

specifies the last word of the field,

put word -2 of field "text"

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

put word 4 of line 250 of myText

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:

put char 4 of word 9 of line 3 of field "text"

but this is not

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

answer char 2 to 4 of word 1 of field "text"

or for the last 5 chars:

answer char -5 to -1 of word 1 of field "text"