LessonsBeginner: First stepsLesson 14

Chunks: chars, words, items and lines

xTalk's superpower for text: pick out any character, word, item or line by its number, count them, and use the same words on fields and files.

  • Beginner
  • Lesson 14 of 83
  • About 10 minutes
  • 7 examples

Asking for a piece

Most languages make you count positions and cut strings with functions. xTalk lets you ask for a piece of text in words: word 2 of, char 1 to 3 of, the last line of. These are called chunk expressions, and they are one of the reasons people love the language.

ExampleRun it in the playground
put "The quick brown fox jumps" into tText
put word 2 of tText
put word 2 to 3 of tText
put char 1 to 3 of tText
put the last word of tText
put the first char of tText
put the number of words of tText
Output
quick
quick brown
The
jumps
T
5

The four chunks

ChunkA piece of text separated by
charnothing: one character (character also works)
wordspaces, tabs or new lines (and a quoted phrase counts as one word)
itemcommas, or the itemDelimiter
linenew lines, or the lineDelimiter
ExampleRun it in the playground
put "red,green,blue" into tColours
put item 2 of tColours
put the number of items of tColours
put "apples" & return & "pears" & return & "plums" into tFruit
put line 3 of tFruit
put the number of lines of tFruit
Output
green
3
plums
3

Counting from the end

A negative number counts from the end: -1 is the last, -2 the one before. the last and the first read even better when that is what you mean.

ExampleRun it in the playground
put "The quick brown fox jumps" into tText
put word -2 of tText
put char -3 to -1 of tText
put the last char of word 1 of tText
Output
fox
mps
e

As the last line shows, chunks go inside chunks: a character of a word of a line of a field is a perfectly good thing to ask for.

Other separators

Not every list uses commas. Set the itemDelimiter to any character to split on it. Like caseSensitive, it goes back to a comma when the handler ends. (The lineDelimiter works the same way for lines.)

ExampleRun it in the playground
put "home/ada/notes.txt" into tPath
set the itemDelimiter to "/"
put item -1 of tPath
put item 1 to -2 of tPath
set the itemDelimiter to tab
put item 2 of ("Ada" & tab & "36" & tab & "London")
Output
notes.txt
home/ada
36

Words and punctuation

A word is anything between spaces, punctuation and all. When you want the words a person would count, use trueWord, which knows about punctuation (and about languages without spaces).

ExampleRun it in the playground
put "Hello, world! (again)" into tText
put word 2 of tText
put trueWord 2 of tText
put the number of trueWords of tText
Output
world!
world
3

Asking for too much

Asking for a chunk that is not there is not an error: you just get empty. That makes loops and checks simple.

ExampleRun it in the playground
put "one two" into tText
put word 5 of tText is empty
put the number of items of "a,b,"
Output
true
2

Try it yourself

From the file name holiday.photo.2026.jpg, put the file's extension (the part after the last dot) and then its name without the extension.

Show a solution
ExampleRun it in the playground
put "holiday.photo.2026.jpg" into tFile
set the itemDelimiter to "."
put item -1 of tFile
put item 1 to -2 of tFile
Output
jpg
holiday.photo.2026

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.