# Working with Text

The [Text](../variable-data-types/the-text-variable-data-type) type in Airscript is used to hold written text that might be displayed to the user in a [Label Web Control](../web-controls/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 (**"**)

```javascript
"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](../airscript-expressions/list-functions/length) function. For example, the length of the string **"Hello world"** is **12**.

```javascript
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_](../variable-data-types/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](../airscript-expressions/text-functions/trim) function removes spaces, tabs, newlines, and other whitespace from the beginning and end of a string.

```javascript
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](../airscript-expressions/text-functions/rept) and [PADSTRING](../airscript-expressions/text-functions/padstring) functions for this.

It is also fairly routine to ensure a string contains only lowercase or uppercase characters. The [LOWERCASE](../airscript-expressions/text-functions/lowercase) and [UPPERCASE](../airscript-expressions/text-functions/uppercase) functions will do just that.

```javascript
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](../airscript-expressions/text-functions/substring) function extracts substrings from a larger string using a starting and ending position inside the string.

```javascript
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](../airscript-expressions/text-functions/string_find) function to find the beginning of a substring. If the result is **-1** it means the substring was not found.

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

You may be interested in whether one string is alphabetically smaller than another. The [STRING\_COMPARE](../airscript-expressions/text-functions/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

```javascript
STRING_COMPARE("Hello", "world") => negative  
STRING_COMPARE("world", "Hello") => positive  
STRING_COMPARE("world", "world") => 0  
STRING_COMPARE("world", "World") => negative  
STRING_COMPARE("World", "world") => positive  
STRING_COMPARE("wo", "world") => positive
```

### More Utilities

The [CONCAT](../airscript-expressions/text-functions/concat) function will combine two or more strings.

```javascript
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](../airscript-expressions/text-functions/replace) or [SUBSTITUTE](../airscript-expressions/text-functions/substitute). The [REPLACE](../airscript-expressions/text-functions/replace) function will replace the first occurrence of the substring, while the [SUBSTITUTE](../airscript-expressions/text-functions/substitute) function will replace any number of occurrences.

```javascript
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](../airscript-expressions/list-functions/join) function to combine multiple strings with a particular separator.

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

The [SPLIT](../airscript-expressions/text-functions/split) function is the opposite of the [JOIN](../airscript-expressions/list-functions/join) function, in that it turns a string into multiple strings by splitting on a separator.

```javascript
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](../airscript-expressions/datatype-functions/to_json)                            | NULL, Boolean, Number, String, Object List | JSON                                       |
| [FROM\_JSON](../airscript-expressions/datatype-functions/from_json)                        | JSON String                                | NULL, Boolean, Number, String, Object List |
| [FORMAT\_NUMBER](../airscript-expressions/number-formatting-functions/format_number)       | Number                                     | Configurable                               |
| [PARSE\_NUMBER](../airscript-expressions/number-formatting-functions/parse_number)         | Configurable                               | Number                                     |
| [FORMAT\_PHONE](../airscript-expressions/phone-functions/format_phone)                     | Phone Number                               | E-164 String ("+15551234567")              |
| [PARSE\_PHONE](../airscript-expressions/phone-functions/parse_phone)                       | E-164 String ("+15551234567")              | Phone Number                               |
| [FORMAT\_DATETIME](../airscript-expressions/datetime-functions/format_datetime)            | Datetime                                   | RFC 3339 timestamp (Configurable)          |
| [DATETIME\_FROM\_FORMAT](../airscript-expressions/datetime-functions/datetime_from_format) | RFC 3339 timestamp (Configurable)          | Datetime                                   |
| [FORMAT\_DATE](../airscript-expressions/datetime-functions/format_date)                    | Date                                       | RFC 3339 timestamp (Configurable)          |
| [DATE\_FROM\_FORMAT](../airscript-expressions/datetime-functions/date_from_format)         | RFC 3339 timestamp (Configurable)          | Date                                       |
| [FORMAT\_TIME](../airscript-expressions/datetime-functions/format_time)                    | Time                                       | RFC 3339 timestamp (Configurable)          |
| [TIME\_FROM\_FORMAT](../airscript-expressions/datetime-functions/time_from_format)         | RFC 3339 timestamp (Configurable)          | Time                                       |

### Relevant Functions

* [SPLIT](../airscript-expressions/text-functions/split)
* [SUBSTRING](../airscript-expressions/text-functions/substring)
* [LOWERCASE](../airscript-expressions/text-functions/lowercase)
* [FORMAT\_TIME](../airscript-expressions/datetime-functions/format_time)
* [FORMAT\_DATETIME](../airscript-expressions/datetime-functions/format_datetime)
* [FORMAT\_DATE](../airscript-expressions/datetime-functions/format_date)
* [ISSTRING](../airscript-expressions/text-functions/isstring)
* [LENGTH](../airscript-expressions/list-functions/length)
* [CONTAINS](../airscript-expressions/list-functions/contains)
* [REPLACE](../airscript-expressions/text-functions/replace)
* [SUBSTITUTE](../airscript-expressions/text-functions/substitute)
* [PADSTRING](../airscript-expressions/text-functions/padstring)
* [CONCAT](../airscript-expressions/text-functions/concat)
* [PARSE\_NUMBER](../airscript-expressions/number-formatting-functions/parse_number)
* [UPPERCASE](../airscript-expressions/text-functions/uppercase)
* [TO\_JSON](../airscript-expressions/datatype-functions/to_json)
* [PARSE\_PHONE](../airscript-expressions/phone-functions/parse_phone)
* [TITLECASE](../airscript-expressions/text-functions/titlecase)
* [STRIP](../airscript-expressions/text-functions/strip)
* [STRING\_COMPARE](../airscript-expressions/text-functions/string_compare)
* [REPT](../airscript-expressions/text-functions/rept)
* [TRIM](../airscript-expressions/text-functions/trim)
* [STRING\_FIND](../airscript-expressions/text-functions/string_find)
* [FORMAT\_NUMBER](../airscript-expressions/number-formatting-functions/format_number)
* [DATETIME\_FROM\_FORMAT](../airscript-expressions/datetime-functions/datetime_from_format)
* [DATE\_FROM\_FORMAT](../airscript-expressions/datetime-functions/date_from_format)
* [TIME\_FROM\_FORMAT](../airscript-expressions/datetime-functions/time_from_format)
* [PARSE\_PHONE](../airscript-expressions/phone-functions/parse_phone)
* [FORMAT\_PHONE](../airscript-expressions/phone-functions/format_phone)
