# Trabalhando com 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:vermelho;">**LEGENDA:**</mark>

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- Parâmetros obrigatórios</mark>

<details>

<summary>Descrição</summary>

**substring(str, n1, n2)** - para aparar uma string

Parâmetros:

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

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**n1**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- número de caracteres a aparar da esquerda</mark> (> 0)

**n2** - número de caracteres a aparar da direita (< 0)

**endswith(str, substr)** - para verificar se a string termina com a substring fornecida

Parâmetros:

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

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**substr**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- строка de pesquisa</mark> - "o que pesquisar"

**startswith(str, substr)** - para verificar se a string começa com a substring fornecida

Parâmetros:

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

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**substr**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- строка de pesquisa</mark> - "o que pesquisar"

**contains(str, substr,registr)** – para verificar se a primeira string contém a segunda string

Parâmetros:

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

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**substr**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- строка de pesquisa</mark> - "o que pesquisar"

**registr** - uma flag que indica se a sensibilidade a maiúsculas e minúsculas deve ser considerada (False significa que não deve ser considerada)

**len(str)** – para contar o número de caracteres em uma string.

Parâmetros:

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

**concat(str1, str2)** – para concatenar (juntar) as strings passadas como parâmetros

Parâmetros:

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

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

**splitter(str, s, n)** - para dividir uma string em partes. A função retorna um array de elementos

Parâmetros:

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

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**s**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- delimitador da string</mark>

**n** - número máximo de elementos

**lower(str)** – para converter uma string para minúsculas

Parâmetros:

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

**upper(str)** - para converter uma string para maiúsculas

Parâmetros:

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

**strip(str)** - para remover espaços em branco de ambas as extremidades de uma string

Parâmetros:

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

**capitalize(str)** - para substituir o primeiro caractere de uma string por sua equivalente em maiúscula (capitaliza a primeira letra da palavra).

Parâmetros:

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

**title(str)** - para converter cada palavra na string str de modo que a primeira letra fique maiúscula e as demais minúsculas

**normalizePhone(str)** - para formatar um número de telefone em um formato padrão: remove todos os caracteres não numéricos e substitui o dígito inicial 8 por 7, se presente

Parâmetros:

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**str**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- string original com um número de telefone</mark>

**replace(str, s1, s2, n)** - para substituir uma substring em uma string por outra substring

Parâmetros:

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

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**s1**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- substring a ser substituída</mark>

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**s2**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">- substring pela qual substituir</mark>

**n** - número de substituições

\
**base64(str)** – para codificar uma string no formato base64

Parâmetros:

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

**base64decode(str)** - para decodificar base64 de volta para uma string

Parâmetros:

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

**urlencode(str) -** para codificar uma string de modo que ela seja segura para transmissão HTTP

Parâmetros:

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

Por exemplo: *John Smith* torna-se *John%20Smith*, e *Anna\&Maria* torna-se *Anna%26Maria*

**urldecode(str)** - para decodificar uma string codificada em URL

Parâmetros:

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

Por exemplo: John%20Smith será traduzido como John Smith

**hmac\_hexdigest(secret\_key, msg, hash\_type)** - para gerar hash de uma string usando 'sha256', 'md5', 'sha512' ou 'sha1'

Parâmetros:

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**secret\_key**</mark> - chave\ <mark style="color:vermelho;">**!**</mark>  <mark style="color:vermelho;">**msg**</mark> - string a ser hasheada\ <mark style="color:vermelho;">**!**</mark>  <mark style="color:vermelho;">**hash\_type**</mark> - tipo de hash ('sha256', 'md5', 'sha512' ou 'sha1')

**select\_random(str, s) -** para selecionar um elemento aleatório de uma string delimitada. O primeiro parâmetro é a string com os elementos, o segundo é o delimitador. O segundo parâmetro tem como padrão '|'.

Parâmetros:

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

<mark style="color:vermelho;">**!**</mark> <mark style="color:vermelho;">**s**</mark> <mark style="color:vermelho;"></mark><mark style="color:vermelho;">-</mark> delimitador da string (o padrão é '|')

Exemplo de uso:&#x20;`select_random('first element | second element | another element | and another one')`

**tg\_escape(str)** - para escapar uma variável e exibi-la em uma mensagem do Telegram com marcação habilitada. A função adiciona uma barra invertida antes dos seguintes caracteres:&#x20;'\_', '\*', '\[', ']', '(', ')', '\~', '', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'

Parâmetros:

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

</details>

<details>

<summary>Exemplos</summary>

Vamos analisar a função de aparo de strings:

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

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

Determinando o comprimento da string:

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

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

Uma função que divide uma string em partes é frequentemente necessária ao trabalhar com tabelas:

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

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

Processamento de número de telefone:

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

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

Substituindo uma substring em uma string:

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

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

</details>

<details>

<summary>Exemplo de código para copiar</summary>

<pre><code><strong>/*Analisando 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)

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

/*dividir a string em partes*/
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}")

/*processamento de número de telefone*/
phone = normalizePhone("+971 50 123 4567")

/*substituição de substring em uma 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/doc/pt/chatbot/functions/calculadora/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.
