# Working with strings

substring() | endswith() | startswith() | contains() | len() | concat() | splitter() | lower() | upper() | strip() | capitalize() | title() | normalizePhone() | replace() | base64() | base64decode() | urlencode() | urldecode() | hmac\_hexdigest() | select\_random() | tg\_escape()

<mark style="color:red;">**LEGEND:**</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;"></mark><mark style="color:red;">- Required parameters</mark>

<details>

<summary>Description</summary>

**substring(str, n1, n2)** -  to trim a string

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**n1**</mark> <mark style="color:red;"></mark><mark style="color:red;">- number of characters to trim from the left</mark> (> 0)

**n2** - number of characters to trim from the right (< 0)

**endswith(str, substr)** - to check if the string ends with the given substring

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark> - "where to search"

<mark style="color:red;">**!**</mark> <mark style="color:red;">**substr**</mark> <mark style="color:red;"></mark><mark style="color:red;">- строка поиска</mark> - "what to search"

**startswith(str, substr)** - to check if the string starts with the given substring

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark> - "where to search"

<mark style="color:red;">**!**</mark> <mark style="color:red;">**substr**</mark> <mark style="color:red;"></mark><mark style="color:red;">- строка поиска</mark> - "what to search"

**contains(str, substr,registr)** – to check if the first string contains the second string

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark> - "where to search"

<mark style="color:red;">**!**</mark> <mark style="color:red;">**substr**</mark> <mark style="color:red;"></mark><mark style="color:red;">- строка поиска</mark> - "what to search"

**registr** - a flag indicating whether to consider case sensitivity (False means case should not be considered)

**len(str)** – to count the number of characters in a string.

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

**concat(str1, str2)** – to concatenate (join) the strings passed as parameters

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str1**</mark> <mark style="color:red;"></mark><mark style="color:red;">- string 1</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str2**</mark> <mark style="color:red;"></mark><mark style="color:red;">- string 2</mark>

**splitter(str, s, n)** -  to split a string into parts. The function returns an array of elements

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s**</mark> <mark style="color:red;"></mark><mark style="color:red;">- string delimiter</mark>

**n** - maximum number of elements

**lower(str)** – to convert a string to lowercase

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

**upper(str)** -  to convert a string to uppercase

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

**strip(str)** -  to trim whitespace from both ends of a string

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

**capitalize(str)** -  to replace the first character of a string with its uppercase equivalent (capitalizes the first letter of the word).

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

**title(str)** - to convert each word in the string str so that the first letter is uppercase and others are lowercase

**normalizePhone(str)** -  to formate a phone number into a standard form: removes all non-digit characters and replaces the starting digit 8 with 7 if present

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string with a phone number</mark>

**replace(str, s1, s2, n)** -  to replace a substring in a string with another substring

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s1**</mark> <mark style="color:red;"></mark><mark style="color:red;">- substring to be replaced</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s2**</mark> <mark style="color:red;"></mark><mark style="color:red;">- substring to replace with</mark>

**n** - number of replacements

\
**base64(str)** – to encode a string in base64 format

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

**base64decode(str)** -  to decode base64 back to a string

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

**urlencode(str) -** to encode a string to make it safe for HTTP transmission

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

For examle: *John Smith* becomes *John%20Smith*, а *Anna\&Maria* becomes *Anna%26Maria*

**urldecode(str)** - to decode a URL-encoded string

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

For example: John%20Smith will be translated as John Smith

**hmac\_hexdigest(secret\_key, msg, hash\_type)** - to hash a string using 'sha256', 'md5', 'sha512', or 'sha1'

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**secret\_key**</mark> - key\ <mark style="color:red;">**!**</mark>  <mark style="color:red;">**msg**</mark> - string to be hashed\ <mark style="color:red;">**!**</mark>  <mark style="color:red;">**hash\_type**</mark> - hash type ( 'sha256', 'md5', 'sha512' or 'sha1')

**select\_random(str, s) -** to select a random element from a delimited string. The first parameter is the string with elements, the second is the delimiter. The second parameter defaults to '|'.

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s**</mark> <mark style="color:red;"></mark><mark style="color:red;">-</mark> string delimiter (default is '|')

Example usage:&#x20;`select_random('first element | second element | another element | and another one')`

**tg\_escape(str)** - to escape a variable and display it in a Telegram message with enabled markup. The function adds a backslash before the following characters:&#x20;'\_', '\*', '\[', ']', '(', ')', '\~', '', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'

Parameters:

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- original string</mark>

</details>

<details>

<summary>Examples</summary>

Let’s analyze the string trimming function:

<div data-with-frame="true"><figure><img src="/files/0tLbArkEARjgjffFh56X" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/IytmSVpUMeUXCA0aXsMj" alt="" width="375"><figcaption></figcaption></figure></div>

Determining the length of the string:

<div data-with-frame="true"><figure><img src="/files/F7IN4yg1pK6hsBMeyYQJ" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/QjvkGvQdorF6kIcuXOyW" alt="" width="375"><figcaption></figcaption></figure></div>

A function that splits a string into parts is often needed when working with tables:

<div data-full-width="true" data-with-frame="true"><figure><img src="/files/rZYMktegteI8yLe5nLds" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-full-width="true" data-with-frame="true"><figure><img src="/files/7Wibovl8U2TJvzwV2liq" alt="" width="375"><figcaption></figcaption></figure></div>

Phone number processing:

<div data-with-frame="true"><figure><img src="/files/ZaZk4wssseWz5TkmIRqF" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/uZjImPNF1jv9WlUpjbDF" alt="" width="375"><figcaption></figcaption></figure></div>

Replacing a substring in a string:

<div data-full-width="true" data-with-frame="true"><figure><img src="/files/LuFWXtLQ3kaHQAq2Ufan" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-full-width="true" data-with-frame="true"><figure><img src="/files/wfKLWP907UwdgxL7xN9i" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example for copying</summary>

<pre><code><strong>/*Analyzing substring()*/
</strong>text = 'text for trimming'
a=substring(text, 4)
a1=substring(text, -4)
b=substring(text, 4, 6)
b1=substring(text, 0, 6)
c=substring(text, 0, -4)
d=substring(text, 4, -4)

/*working with len()*/
text = 'text to be trimmed'
a=len(text)
b=len("what a wonderful world!")

/*split the string into parts*/
elements = splitter('s, W, q', ',')
text='text1:text2:text3:text4:text5'
texts = splitter(text, ':',2)

/*title()*/
full name = John Smith
full name = title("#{full name}")

/*phone number processing*/
phone = normalizePhone("+971 50 123 4567")

/*substring replacement in a string*/
a=replace("wwww2222ww", "w", "e", 1)


</code></pre>

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mavibot.ai/chatbot/functions/calculator/strings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
