5.3. Getting the Size of a String

Sometimes you want to know the number of characters in a string. You can easily find this out by using the count command, followed by the string. For example, writing

count "Hello"

gives a result of 5, because that's the number of characters in the string. The pair of double quotes doesn't count; they're just there to delimit the characters in the string.

Obviously, you can use the count command to get the number of characters in a string stored inside a variable. So given the following statement:

set message to "AppleScript rocks!"

the statement

log count message

puts the value 18 into the log because that's the number of characters in the string stored in the variable message.

You learned that the null string is a string that contains no characters. That means you can predict that the expression

count ""

returns zero, which is, in fact, the case.

Later in this chapter, you see how to use the count command to count the number of elements in a list.

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.