2020-04-03 13:23:32 +00:00
---
toc_priority: 47
toc_title: Splitting and Merging Strings and Arrays
---
2020-04-30 18:19:18 +00:00
# Functions for Splitting and Merging Strings and Arrays {#functions-for-splitting-and-merging-strings-and-arrays}
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## splitByChar(separator, s) {#splitbycharseparator-s}
2017-12-28 15:13:23 +00:00
2020-03-20 05:37:46 +00:00
Splits a string into substrings separated by a specified character. It uses a constant string `separator` which consisting of exactly one character.
2017-12-28 15:13:23 +00:00
Returns an array of selected substrings. Empty substrings may be selected if the separator occurs at the beginning or end of the string, or if there are multiple consecutive separators.
2020-03-20 05:37:46 +00:00
**Syntax**
2020-04-30 18:19:18 +00:00
``` sql
2021-05-25 13:03:35 +00:00
splitByChar(separator, s)
2020-03-20 05:37:46 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-03-20 05:37:46 +00:00
2020-04-30 18:19:18 +00:00
- `separator` — The separator which should contain exactly one character. [String ](../../sql-reference/data-types/string.md ).
- `s` — The string to split. [String ](../../sql-reference/data-types/string.md ).
2020-03-20 05:37:46 +00:00
**Returned value(s)**
Returns an array of selected substrings. Empty substrings may be selected when:
2020-04-30 18:19:18 +00:00
- A separator occurs at the beginning or end of the string;
- There are multiple consecutive separators;
- The original string `s` is empty.
2020-03-20 05:37:46 +00:00
2021-05-25 13:03:35 +00:00
Type: [Array ](../../sql-reference/data-types/array.md )([String](../../sql-reference/data-types/string.md)).
2020-03-20 05:37:46 +00:00
**Example**
2020-03-19 02:35:18 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-05-25 13:03:35 +00:00
SELECT splitByChar(',', '1,2,3,abcde');
2020-03-19 02:35:18 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2020-03-19 02:35:18 +00:00
┌─splitByChar(',', '1,2,3,abcde')─┐
│ ['1','2','3','abcde'] │
└─────────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
## splitByString(separator, s) {#splitbystringseparator-s}
2020-03-20 05:37:46 +00:00
Splits a string into substrings separated by a string. It uses a constant string `separator` of multiple characters as the separator. If the string `separator` is empty, it will split the string `s` into an array of single characters.
**Syntax**
2020-04-30 18:19:18 +00:00
``` sql
2021-05-25 13:03:35 +00:00
splitByString(separator, s)
2020-03-20 05:37:46 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-03-20 05:37:46 +00:00
2020-04-30 18:19:18 +00:00
- `separator` — The separator. [String ](../../sql-reference/data-types/string.md ).
- `s` — The string to split. [String ](../../sql-reference/data-types/string.md ).
2020-03-20 05:37:46 +00:00
**Returned value(s)**
Returns an array of selected substrings. Empty substrings may be selected when:
2021-05-25 13:03:35 +00:00
Type: [Array ](../../sql-reference/data-types/array.md )([String](../../sql-reference/data-types/string.md)).
2020-03-20 18:36:14 +00:00
2020-04-30 18:19:18 +00:00
- A non-empty separator occurs at the beginning or end of the string;
- There are multiple consecutive non-empty separators;
- The original string `s` is empty while the separator is not empty.
2017-12-28 15:13:23 +00:00
2020-03-20 05:37:46 +00:00
**Example**
2020-03-19 02:35:18 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-05-25 13:03:35 +00:00
SELECT splitByString(', ', '1, 2 3, 4,5, abcde');
2020-03-19 02:35:18 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2020-03-19 02:35:18 +00:00
┌─splitByString(', ', '1, 2 3, 4,5, abcde')─┐
│ ['1','2 3','4,5','abcde'] │
└───────────────────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
``` sql
2021-05-25 13:03:35 +00:00
SELECT splitByString('', 'abcde');
2020-03-19 02:35:18 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2020-03-19 02:35:18 +00:00
┌─splitByString('', 'abcde')─┐
│ ['a','b','c','d','e'] │
└────────────────────────────┘
```
2017-12-28 15:13:23 +00:00
2021-05-13 03:15:38 +00:00
## splitByRegexp(regexp, s) {#splitbyregexpseparator-s}
2021-05-19 10:21:34 +00:00
Splits a string into substrings separated by a regular expression. It uses a regular expression string `regexp` as the separator. If the `regexp` is empty, it will split the string `s` into an array of single characters. If no match is found for this regular expression, the string `s` won't be split.
2021-05-13 03:15:38 +00:00
**Syntax**
``` sql
2021-05-25 13:03:35 +00:00
splitByRegexp(regexp, s)
2021-05-13 03:15:38 +00:00
```
**Arguments**
- `regexp` — Regular expression. Constant. [String ](../data-types/string.md ) or [FixedString ](../data-types/fixedstring.md ).
- `s` — The string to split. [String ](../../sql-reference/data-types/string.md ).
**Returned value(s)**
Returns an array of selected substrings. Empty substrings may be selected when:
- A non-empty regular expression match occurs at the beginning or end of the string;
- There are multiple consecutive non-empty regular expression matches;
- The original string `s` is empty while the regular expression is not empty.
2021-05-25 13:03:35 +00:00
Type: [Array ](../../sql-reference/data-types/array.md )([String](../../sql-reference/data-types/string.md)).
2021-05-19 10:21:34 +00:00
2021-05-13 03:15:38 +00:00
**Example**
2021-05-19 10:21:34 +00:00
Query:
2021-05-13 03:15:38 +00:00
``` sql
2021-05-25 13:03:35 +00:00
SELECT splitByRegexp('\\d+', 'a12bc23de345f');
2021-05-13 03:15:38 +00:00
```
2021-05-19 10:21:34 +00:00
Result:
2021-05-13 03:15:38 +00:00
``` text
2021-05-13 09:21:00 +00:00
┌─splitByRegexp('\\d+', 'a12bc23de345f')─┐
│ ['a','bc','de','f'] │
└────────────────────────────────────────┘
2021-05-13 03:15:38 +00:00
```
2021-05-19 10:21:34 +00:00
Query:
2021-05-13 03:15:38 +00:00
``` sql
2021-05-25 13:03:35 +00:00
SELECT splitByRegexp('', 'abcde');
2021-05-13 03:15:38 +00:00
```
2021-05-19 10:21:34 +00:00
Result:
2021-05-13 03:15:38 +00:00
``` text
┌─splitByRegexp('', 'abcde')─┐
│ ['a','b','c','d','e'] │
└────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
## arrayStringConcat(arr\[, separator\]) {#arraystringconcatarr-separator}
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Concatenates the strings listed in the array with the separator.’ separator’ is an optional parameter: a constant string, set to an empty string by default.
2017-12-28 15:13:23 +00:00
Returns the string.
2020-03-20 10:10:48 +00:00
## alphaTokens(s) {#alphatokenss}
2017-12-28 15:13:23 +00:00
Selects substrings of consecutive bytes from the ranges a-z and A-Z.Returns an array of substrings.
2020-03-20 05:37:46 +00:00
**Example**
2018-09-21 15:13:45 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-05-25 13:03:35 +00:00
SELECT alphaTokens('abca1abc');
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-21 15:13:45 +00:00
┌─alphaTokens('abca1abc')─┐
│ ['abca','abc'] │
└─────────────────────────┘
2018-10-16 10:47:17 +00:00
```
2020-03-20 10:10:48 +00:00
2020-07-19 09:33:50 +00:00
## extractAllGroups(text, regexp) {#extractallgroups}
Extracts all groups from non-overlapping substrings matched by a regular expression.
**Syntax**
``` sql
extractAllGroups(text, regexp)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-07-19 09:33:50 +00:00
- `text` — [String ](../data-types/string.md ) or [FixedString ](../data-types/fixedstring.md ).
- `regexp` — Regular expression. Constant. [String ](../data-types/string.md ) or [FixedString ](../data-types/fixedstring.md ).
**Returned values**
- If the function finds at least one matching group, it returns `Array(Array(String))` column, clustered by group_id (1 to N, where N is number of capturing groups in `regexp` ).
- If there is no matching group, returns an empty array.
Type: [Array ](../data-types/array.md ).
**Example**
Query:
``` sql
SELECT extractAllGroups('abc=123, 8="hkl"', '("[^"]+"|\\w+)=("[^"]+"|\\w+)');
```
Result:
``` text
┌─extractAllGroups('abc=123, 8="hkl"', '("[^"]+"|\\w+)=("[^"]+"|\\w+)')─┐
│ [['abc','123'],['8','"hkl"']] │
└───────────────────────────────────────────────────────────────────────┘
```