## String Functions

Studio provides a set of functions for working with strings in expressions.

| Function | Arguments Description |
| --- | --- |
| [`substring()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#substring) | Extracts a specified substring from a given string. |
| [`startsWith()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#startswith) | Determines whether a string begins with specific characters. |
| [`endsWith()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#endswith) | Determines whether a string ends with specific characters. |
| [`stringLength()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#stringlength) | Determines the number of characters in the specified string. |
| [`indexOf()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#indexof) | Determines the index of the first occurrence of a substring. |
| [`lastIndexOf()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#lastindexof) | Determines the index of the last occurrence of the specified value, searching backward. |
| [`toUpperCase()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#touppercase) | Convert all characters in a string to upper case. |
| [`toLowerCase()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#tolowercase) | Convert all characters in a string to lower case. |
| [`replace()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#replace) | Replaces substrings within a string. |
| [`contains()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#contains) | Determines whether one string may be found within another string. |
| [`charAt()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#charat) | Get a new string containing the single character unit located at the specified offset. |
| [`padStart()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#padstart) | Pads the front of the input string with another string until the resulting string reaches the given length. |
| [`padEnd()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#padend) | Pads the end of the input string with another string until the resulting string reaches the given length. |
| [`trim()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#trim) | Removes whitespace from both ends of a string. |
| [`trimStart()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#trimstart) | Removes whitespace from the start of a string. |
| [`trimEnd()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#trimend) | Removes whitespace from the end of a string. |
| [`matches()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#matches) | Returns true for strings that match the provided regex. |
| [`firstMatch()`](https://docs.foursquare.com/analytics-products/docs/expressions-string#firstmatch) | Returns the first string that matches given the provided regex. |

### Remarks

- All indices into strings are zero-based, meaning that the first character has index `0`, not `1`.

## API Reference

### substring

Extracts a specified substring from a given string.

TypeScript

```typescript
substring(string: string, startIndex: integer, endIndex?: integer): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The string to extract a substring from. |
| `startIndex` | `int` | The index of the first character to be included. |
| `endIndex` | `int` | The index of the first character to be excluded. If omitted, all characters to the end of the string are included. |

#### Returns

Returns a new string containing the specified part of the given string.

#### Notes

- If startIndex is equal to `endIndex`, `substring()` returns an empty string.
- If `startIndex` is greater than `endIndex`, the effect of `substring()` is as if the two arguments were swapped.

### startsWith

Determines whether a string begins with the characters of another string.

TypeScript

```typescript
startsWith(string: string, searchString: string, startIndex: integer): boolean
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The string to extract a substring from. |
| `searchString` | `string` | The substring to look for. |
| `startIndex` | `int` | The index of the first character to be included. |

#### Returns

Returns `true` or `false` as appropriate.

#### Notes

This method is case-sensitive.

### endsWith

Determines whether a string ends with the characters of a specified string.

TypeScript

```typescript
endsWith(string: string, searchString: string, fromIndex?: integer): boolean
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The string to extract a substring from. |
| `searchString` | `string` | The substring to check for. |
| `fromIndex` | `int` | The index of the last character to be included. Defaults to the end of the string. |

#### Returns

Returns `true` or `false` as appropriate.

#### Notes

This method is case-sensitive.

### stringLength

Determines the number of characters in the specified string.

TypeScript

```typescript
stringLength(string: string): integer
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The string to check for length. |

#### Returns

Returns the number of characters in the given string.

### indexOf

Determines the index of the first occurrence of a substring.

TypeScript

```typescript
indexOf(string: string, substring: string, startIndex?: integer): integer
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The string to find a substring in. |
| `substring` | `string` | The string to check for. |
| `fromIndex` | `int` | The index to search backward from. Defaults to end of string. |

#### Returns

Returns the index of the first occurrence of `substring`; -1 if not found.

### lastIndexOf

Determines the index of the last occurrence of the specified value, searching backward.

TypeScript

```typescript
lastIndexOf(string: string, searchString: string, fromIndex?: integer): integer
```

#### Returns

Returns the index of the last occurrence of `substring`; -1 if not found.

### toUpperCase

Convert all characters in a string to upper case.

TypeScript

```typescript
toUpperCase(string: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string to convert to upper case. |

#### Returns

Returns a new `string` with all lower case characters replaced with upper case characters.

#### Notes

The specifics of case conversion may depend on the current locale, determined by the browser and the operating system.

### toLowerCase

Convert all characters in a string to lower case.

TypeScript

```typescript
toLowerCase(string: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string to convert to lower case. |

#### Returns

Returns a new `string` with all upper case characters replaced with lower case characters.

#### Notes

The specifics of case conversion may depend on the current locale, determined by the browser and the operating system.

### replace

Replaces substrings within a string.

TypeScript

```typescript
replace(string: string, searchString: string, newSubstring: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string containing a substring to be replaced. |
| `searchString` | `string` | The substring to be replaced. |
| `newSubstring` | `string` | The replacement string. |

#### Returns

Returns a string with the replaced substring, or an unaltered string if no `searchString` was found.

### contains

Determines whether one string may be found within another string.

TypeScript

```typescript
contains(string: string, searchString: string): boolean
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string to evaluate for a substring. |
| `searchString` | `string` | The substring to search for. |

#### Returns

Returns `true` if the `searchString` is found inside the supplied string, otherwise `false`.

### charAt

Get a new string consisting of the single character unit located at a specified offset.

TypeScript

```typescript
charAt(string: string, index: integer): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string. |
| `index` | `int` | The index of the character. |

#### Returns

Returns a string containing the character at `index` at the specified index. If `index` is out of range, an empty string is returned.

### padStart

Pads the front of the input string with another string until the resulting string reaches the given length.

TypeScript

```typescript
padStart(string: string, targetLength: integer, padString?: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string. |
| `targetLength` | `int` | The minimum length of the returned string. |
| `padString` | `string` | Optional. Applied (multiple times, if needed) from the start of the current string. Defaults to `' '` (Unicode Character U+0020 'SPACE'). |

#### Returns

Returns a string that has at least `targetLength` characters.

### padEnd

Pads the end of the input string with another string until the resulting string reaches the given length.

TypeScript

```typescript
padEnd(string: string, targetLength: integer, padString?: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string. |
| `targetLength` | `int` | The minimum length of the returned string. |
| `padString` | `string` | Optional. Applied (multiple times, if needed) from the end of the current string. Defaults to `' '` (Unicode Character U+0020 'SPACE'). |

#### Returns

Returns a string that has at least `targetLength` characters.

### trim

Removes whitespace from both ends of a string.

TypeScript

```typescript
trim(string: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string. |

#### Returns

Returns a string without leading and trailing whitespace.

### trimStart

Removes whitespace from the start of a string.

TypeScript

```typescript
trimStart(string: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string. |

#### Returns

Returns a string with leading whitespace stripped.

### trimEnd

Removes whitespace from the end of a string.

TypeScript

```typescript
trimEnd(string: string): string
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string. |

#### Returns

Returns a string with trailing whitespace stripped.

### matches

Determines whether a string matches the provided regex.

TypeScript

```typescript
matches(string: string, regex: string): boolean
```

| Parameter | Type | Description |
| --- | --- | --- |
| `string` | `string` | The input string. |
| `regex` | `string` | The regex argument to use in a search. |

#### Returns

Returns `true` if a string matches the provided regex, `false` otherwise.

### firstMatch

Finds the first string that matches the provided regex.

TypeScript

```typescript
firstMatch(string: string, regex: string): string
```

#### Returns

Returns the first string that matches the provided regex.
