Functions for working with strings

**Examples**

**Usage example**

```
SELECT sparseGramsHashesUTF8('алиса', 3)
```

```
┌─sparseGramsHashesUTF8('алиса', 3)─┐
│ [4178533925,3855635300,561830861] │
└───────────────────────────────────┘
```

## sparseGramsUTF8[​](#sparseGramsUTF8 "Direct link to sparseGramsUTF8")

Introduced in: v25.5.0

Finds all substrings of a given UTF-8 string that have a length of at least `n`, where the hashes of the (n-1)-grams at the borders of the substring are strictly greater than those of any (n-1)-gram inside the substring.
Expects a UTF-8 string, throws an exception in case of an invalid UTF-8 sequence.
Uses `CRC32` as a hash function.

**Syntax**

```
sparseGramsUTF8(s[, min_ngram_length[, max_ngram_length[, min_cutoff_length]]])
```

**Arguments**

- `s` — An input string. [`String`](/docs/sql-reference/data-types/string)
- `min_ngram_length` — Optional. The minimum length of extracted ngram. The default and minimal value is 3. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `max_ngram_length` — Optional. The maximum length of extracted ngram. The default value is 100. Should be not less than `min_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `min_cutoff_length` — Optional. If specified, only n-grams with length greater or equal than `min_cutoff_length` are returned. The default value is the same as `min_ngram_length`. Should be not less than `min_ngram_length` and not greater than `max_ngram_length`. [`UInt*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns an array of selected UTF-8 substrings. [`Array(String)`](/docs/sql-reference/data-types/array)

**Examples**

**Usage example**

```
SELECT sparseGramsUTF8('алиса', 3)
```

```
┌─sparseGramsUTF8('алиса', 3)─┐
│ ['али','лис','иса']         │
└─────────────────────────────┘
```

## startsWith[​](#startsWith "Direct link to startsWith")

Introduced in: v1.1.0

Checks whether a string begins with the provided string.

**Syntax**

```
startsWith(s, prefix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `prefix` — Prefix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` starts with `prefix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT startsWith('ClickHouse', 'Click');
```

```
┌─startsWith('⋯', 'Click')─┐
│                        1 │
└──────────────────────────┘
```

## startsWithCaseInsensitive[​](#startsWithCaseInsensitive "Direct link to startsWithCaseInsensitive")

Introduced in: v25.9.0

Checks whether a string begins with the provided case-insensitive string.

**Syntax**

```
startsWithCaseInsensitive(s, prefix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `prefix` — Case-insensitive prefix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` starts with case-insensitive `prefix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT startsWithCaseInsensitive('ClickHouse', 'CLICK');
```

```
┌─startsWithCaseInsensitive('⋯', 'CLICK')─┐
│                                       1 │
└─────────────────────────────────────────┘
```

## startsWithCaseInsensitiveUTF8[​](#startsWithCaseInsensitiveUTF8 "Direct link to startsWithCaseInsensitiveUTF8")

Introduced in: v25.9.0

Checks if a string starts with the provided case-insensitive prefix.
Assumes that the string contains valid UTF-8 encoded text.
If this assumption is violated, no exception is thrown and the result is undefined.

**Syntax**

```
startsWithCaseInsensitiveUTF8(s, prefix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `prefix` — Case-insensitive prefix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` starts with case-insensitive `prefix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT startsWithCaseInsensitiveUTF8('приставка', 'при')
```

```
┌─startsWithUT⋯ка', 'при')─┐
│                        1 │
└──────────────────────────┘
```

## startsWithUTF8[​](#startsWithUTF8 "Direct link to startsWithUTF8")

Introduced in: v23.8.0

Checks if a string starts with the provided prefix.
Assumes that the string contains valid UTF-8 encoded text.
If this assumption is violated, no exception is thrown and the result is undefined.

**Syntax**

```
startsWithUTF8(s, prefix)
```

**Arguments**

- `s` — String to check. [`String`](/docs/sql-reference/data-types/string)
- `prefix` — Prefix to check for. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns `1` if `s` starts with `prefix`, otherwise `0`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT startsWithUTF8('приставка', 'при')
```

```
┌─startsWithUT⋯ка', 'при')─┐
│                        1 │
└──────────────────────────┘
```

## stringBytesEntropy[​](#stringBytesEntropy "Direct link to stringBytesEntropy")

Introduced in: v25.6.0

Calculates Shannon's entropy of byte distribution in a string.

**Syntax**

```
stringBytesEntropy(s)
```

**Arguments**

- `s` — The string to analyze. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns Shannon's entropy of byte distribution in the string. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT stringBytesEntropy('Hello, world!')
```

```
┌─stringBytesEntropy('Hello, world!')─┐
│                         3.07049960  │
└─────────────────────────────────────┘
```

## stringBytesUniq[​](#stringBytesUniq "Direct link to stringBytesUniq")

Introduced in: v25.6.0

Counts the number of distinct bytes in a string.

**Syntax**

```
stringBytesUniq(s)
```

**Arguments**

- `s` — The string to analyze. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the number of distinct bytes in the string. [`UInt16`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT stringBytesUniq('Hello')
```

```
┌─stringBytesUniq('Hello')─┐
│                        4 │
└──────────────────────────┘
```

## stringJaccardIndex[​](#stringJaccardIndex "Direct link to stringJaccardIndex")

Introduced in: v23.11.0

Calculates the [Jaccard similarity index](https://en.wikipedia.org/wiki/Jaccard_index) between two byte strings.

**Syntax**

```
stringJaccardIndex(s1, s2)
```

**Arguments**

- `s1` — First input string. [`String`](/docs/sql-reference/data-types/string)
- `s2` — Second input string. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the Jaccard similarity index between the two strings. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT stringJaccardIndex('clickhouse', 'mouse')
```

```
┌─stringJaccardIndex('clickhouse', 'mouse')─┐
│                                       0.4 │
└───────────────────────────────────────────┘
```

## stringJaccardIndexUTF8[​](#stringJaccardIndexUTF8 "Direct link to stringJaccardIndexUTF8")

Introduced in: v23.11.0

Like [`stringJaccardIndex`](#stringJaccardIndex) but for UTF8-encoded strings.

**Syntax**

```
stringJaccardIndexUTF8(s1, s2)
```

**Arguments**

- `s1` — First input UTF8 string. [`String`](/docs/sql-reference/data-types/string)
- `s2` — Second input UTF8 string. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the Jaccard similarity index between the two UTF8 strings. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT stringJaccardIndexUTF8('我爱你', '我也爱你')
```

```
┌─stringJaccardIndexUTF8('我爱你', '我也爱你')─┐
│                                       0.75 │
└─────────────────────────────────────────────┘
```

## substring[​](#substring "Direct link to substring")

Introduced in: v1.1.0

Returns the substring of a string `s` which starts at the specified byte index `offset`.
Byte counting starts from 1 with the following logic: