# Working with Text in Airscript

Sean Lynch

The Text type in Airscript is used to hold written text that might be displayed to the user in a Label Web Control. The text type is also used when capturing information from a user such as a name, an address, or phone number. In addition to using the Text type to store real-world information, the Text type is also often used to store data used by computers, such as a unique identifier or the status of an ongoing sales opportunity. Airkit provides a suite of useful utilities to help work with data of this nature.

Note: In Airscript, as in many other programming languages., a particular instance of the text type is often referred to as a String.

To create Text (a string), enclose the text with the double-quote character (")

"Hello world!"

For the majority of use cases, it is useful to think of a string as a sequence of characters such as a, b, or even special characters such as -, %, or really any other character that might be needed. The number of characters in a string can be obtained with the LENGTH function. For example, the length of the string "Hello world" is 12.

LENGTH("Hello world!") => 12

Note: The definition of a character may not always be what you expect, for example, the "Slightly Smiling Face" emoji (🙂) as well as many non-English characters are actually made up of two or more characters. See The Text Variable Data Type for more information.

## Convenience Utilities

There are a number of routine operations needed for dealing with strings.

When accepting input from a user you may want to remove extra whitespace, for example, if the user enters an extra space at the end of their name you will likely want to remove it so that you store their name as "John Smith" rather than "John Smith ". The TRIM function removes spaces, tabs, newlines, and other whitespace from the beginning and end of a string.

TRIM("  Hello world!   ") => "Hello world!"

There are times that you may want to repeat a given string a certain number of times, for example repeating the string "abc" three times to get the string "abcabcabc". See the REPT and PADSTRING functions for this.

It is also fairly routine to ensure a string contains only lowercase or uppercase characters. The LOWERCASE and UPPERCASE functions will do just that.

LOWERCASE("Hello world!") => "hello world!"UPPERCASE("Hello world!") => "HELLO WORLD!"

## Searching and Ordering

A substring is a smaller string contained inside a larger string. Both "Hello", and "world" are substrings of the string "Hello world". The SUBSTRING function extracts substrings from a larger string using a starting and ending position inside the string.

SUBSTRING("Hello world!", 0, 5) => "Hello"SUBSTRING("Hello world!", 6, 11) => "world"

Although you may not always know ahead of time the position of a substring. In these cases use the STRING_FIND function to find the beginning of a substring. If the result is -1 it means the substring was not found.

STRING_FIND("Hello world!", "Hello") => 0STRING_FIND("Hello world!", "world") => 6STRING_FIND("Hello world!", "bananas") => -1

You may be interested in whether one string is alphabetically smaller than another. The STRING_COMPARE function will determine this by returning a negative number if the first string is smaller than the second, 0 if the strings are the same, and a positive number if the first string is larger than the second

STRING_COMPARE("Hello", "world") => negativeSTRING_COMPARE("world", "Hello") => positiveSTRING_COMPARE("world", "world") => 0STRING_COMPARE("world", "World") => negativeSTRING_COMPARE("World", "world") => positiveSTRING_COMPARE("wo", "world") => positive

## More Utilities

The CONCAT function will combine two or more strings.

CONCAT("Hello ", "world!") => "Hello world!"CONCAT("Hello ", "world", "!") => "Hello world!"CONCAT("Hello", " ", "world", "!") => "Hello world!"

In order to replace a substring with another use REPLACE or SUBSTITUTE. The REPLACE function will replace the first occurrence of the substring, while the SUBSTITUTE function will replace any number of occurrences.

REPLACE("Hello world!", "l", "L") => "HeLlo world!"SUBSTITUTE("Hello world!", "l", "L") => HeLLo worLd!"

## Joining and Splitting

Another way to combine strings is using the JOIN function to combine multiple strings with a particular separator.

JOIN([ "Hello", "world!" ], " ") => "Hello world!"JOIN([ "Hello", "world!" ], ", ") => "Hello, world!"

The SPLIT function is the opposite of the JOIN function, in that it turns a string into multiple strings by splitting on a separator.

SPLIT("Hello world!") => [ "Hello", "world!" ]

## Parsing and Formatting

It is very common for external systems to encode different types as strings. Airkit provides a set of functions to convert between other types and strings.

Function Name From To
TO_JSON NULL, Boolean, Number, String, Object List JSON
FROM_JSON JSON String NULL, Boolean, Number, String, Object List
FORMAT_NUMBER Number Configurable
PARSE_NUMBER Configurable Number
FORMAT_PHONE Phone Number E-164 String ("+15551234567")
PARSE_PHONE E-164 String ("+15551234567") Phone Number
FORMAT_DATETIME Datetime RFC 3339 timestamp (Configurable)
DATETIME_FROM_FORMAT RFC 3339 timestamp (Configurable)
Datetime
FORMAT_DATE Date RFC 3339 timestamp (Configurable)
DATE_FROM_FORMAT RFC 3339 timestamp (Configurable) Date
FORMAT_TIME Time RFC 3339 timestamp (Configurable)
TIME_FROM_FORMAT RFC 3339 timestamp (Configurable) Time