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.

Example Stack

Example Stack

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

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.

Editing Text

Editing Text

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

Item Example

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

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

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

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

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.

5 Comments

William Volkoff

Q. re: retrieve a chunk. I do count 2 char and see an 'e'. I count to word 4 and see 'ut' but not 'sit'? Not sure how line 3 is found?

get char 2 of field "text" -- returns 'e'
get word 4 of field "text" -- returns 'sit'
get line 3 of field "text" -- returns 'Cum sociis natoque penatibus et..."

William Volkoff

get line 3 of field "text" -- returns 'Cum sociis natoque penatibus et..."

My ques: Does line three refer to paragraph? Line 3 in literal lang refers to the 3rd sentence of the 1st paragraph. So I am confused how the jump was made to the 3rd paragraph in the first example.

PS, I did self correct on the previous email I sent with the exception of this current request.

Thank you.

Hanson Schmidt-Cornelius

Hi William,

thank you very much for your comments.

There was indeed a small inconsistency in the inline comments, with some referring to the first text block example and some referring to the second text block example. This has now been addressed in the lesson.

"line 3" refer to the third line of text and does not take account of punctuation. What you are seeing here is the text wrapping around in the text window. If you could stretch the text window far enough in a horizontal direction, then you would find that the text would unwrap into very long lines of text, with "Cum sociis natoque penatibus et..." being on the third line. When wrapped, the text does form a paragraph representation.

Kind Regards,

Hanson

Rory

Is there a way to find the total number of lines in a document?

Elanor Buchanan

Hi Rory

You can use

put the number of lines in into tNumberOfLines

So if your text is in a field you can use

put the number of lines in field "my text" into tNumberOfLines

If your text is in an external file you can get the number of lines in it without putting it into a field using the URL keyword

put the number of lines in url ("file:C:\Users\Name\Desktop\myfile.txt") into tNumberOfLines

Kind regards

Elanor

Add your comment

E-Mail me when someone replies to this comment