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.
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
quick quick brown The jumps T 5
The four chunks
| Chunk | A piece of text separated by |
|---|---|
char | nothing: one character (character also works) |
word | spaces, tabs or new lines (and a quoted phrase counts as one word) |
item | commas, or the itemDelimiter |
line | new lines, or the lineDelimiter |
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
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.
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
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.)
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")
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).
put "Hello, world! (again)" into tText
put word 2 of tText
put trueWord 2 of tText
put the number of trueWords of tText
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.
put "one two" into tText
put word 5 of tText is empty
put the number of items of "a,b,"
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
put "holiday.photo.2026.jpg" into tFile
set the itemDelimiter to "."
put item -1 of tFile
put item 1 to -2 of tFile
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.