# Travailler avec des chaînes de caractères

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;">**LÉGENDE :**</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;"></mark><mark style="color:red;">- Paramètres requis</mark>

<details>

<summary>Description</summary>

**substring(str, n1, n2)** - pour rogner une chaîne

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**n1**</mark> <mark style="color:red;"></mark><mark style="color:red;">- nombre de caractères à rogner à gauche</mark> (> 0)

**n2** - nombre de caractères à rogner à droite (< 0)

**endswith(str, substr)** - pour vérifier si la chaîne se termine par la sous-chaîne donnée

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark> - « où chercher »

<mark style="color:red;">**!**</mark> <mark style="color:red;">**substr**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne de recherche</mark> - « quoi chercher »

**startswith(str, substr)** - pour vérifier si la chaîne commence par la sous-chaîne donnée

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark> - « où chercher »

<mark style="color:red;">**!**</mark> <mark style="color:red;">**substr**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne de recherche</mark> - « quoi chercher »

**contains(str, substr,registr)** – pour vérifier si la première chaîne contient la seconde chaîne

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark> - « où chercher »

<mark style="color:red;">**!**</mark> <mark style="color:red;">**substr**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne de recherche</mark> - « quoi chercher »

**registr** - un indicateur précisant s’il faut tenir compte de la sensibilité à la casse (False signifie qu’il ne faut pas en tenir compte)

**len(str)** – pour compter le nombre de caractères d’une chaîne.

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

**concat(str1, str2)** – pour concaténer (joindre) les chaînes passées en paramètres

Paramètres :

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

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

**splitter(str, s, n)** - pour diviser une chaîne en parties. La fonction renvoie un tableau d’éléments

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s**</mark> <mark style="color:red;"></mark><mark style="color:red;">- délimiteur de chaîne</mark>

**n** - nombre maximal d’éléments

**lower(str)** – pour convertir une chaîne en minuscules

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

**upper(str)** - pour convertir une chaîne en majuscules

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

**strip(str)** - pour supprimer les espaces aux deux extrémités d’une chaîne

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

**capitalize(str)** - pour remplacer le premier caractère d’une chaîne par son équivalent en majuscule (met en majuscule la première lettre du mot).

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

**title(str)** - pour convertir chaque mot de la chaîne str de sorte que la première lettre soit en majuscule et les autres en minuscules

**normalizePhone(str)** - pour formater un numéro de téléphone dans un format standard : supprime tous les caractères non numériques et remplace le chiffre initial 8 par 7 s’il est présent

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d’origine avec un numéro de téléphone</mark>

**replace(str, s1, s2, n)** - pour remplacer une sous-chaîne dans une chaîne par une autre sous-chaîne

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s1**</mark> <mark style="color:red;"></mark><mark style="color:red;">- sous-chaîne à remplacer</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s2**</mark> <mark style="color:red;"></mark><mark style="color:red;">- sous-chaîne de remplacement</mark>

**n** - nombre de remplacements

\
**base64(str)** – pour encoder une chaîne au format base64

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

**base64decode(str)** - pour décoder du base64 en chaîne

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

**urlencode(str) -** pour encoder une chaîne afin de la rendre sûre pour la transmission HTTP

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

Par exemple : *John Smith* devient *John%20Smith*, et *Anna\&Maria* devient *Anna%26Maria*

**urldecode(str)** - pour décoder une chaîne encodée en URL

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

Par exemple : John%20Smith sera traduit par John Smith

**hmac\_hexdigest(secret\_key, msg, hash\_type)** - pour hacher une chaîne à l’aide de 'sha256', 'md5', 'sha512' ou 'sha1'

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**secret\_key**</mark> - clé\ <mark style="color:red;">**!**</mark>  <mark style="color:red;">**msg**</mark> - chaîne à hacher\ <mark style="color:red;">**!**</mark>  <mark style="color:red;">**hash\_type**</mark> - type de hachage ( 'sha256', 'md5', 'sha512' ou 'sha1')

**select\_random(str, s) -** pour sélectionner un élément aléatoire dans une chaîne délimitée. Le premier paramètre est la chaîne contenant les éléments, le second est le délimiteur. Le second paramètre est par défaut '|'.

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

<mark style="color:red;">**!**</mark> <mark style="color:red;">**s**</mark> <mark style="color:red;"></mark><mark style="color:red;">-</mark> délimiteur de chaîne (par défaut '|')

Exemple d’utilisation :&#x20;`select_random('premier élément | deuxième élément | un autre élément | encore un autre')`

**tg\_escape(str)** - pour échapper une variable et l’afficher dans un message Telegram avec le balisage activé. La fonction ajoute un antislash avant les caractères suivants :&#x20;'\_', '\*', '\[', ']', '(', ')', '\~', '', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'

Paramètres :

<mark style="color:red;">**!**</mark> <mark style="color:red;">**str**</mark> <mark style="color:red;"></mark><mark style="color:red;">- chaîne d'origine</mark>

</details>

<details>

<summary>Exemples</summary>

Analysons la fonction de rognage de chaîne :

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

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

Détermination de la longueur de la chaîne :

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

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

Une fonction qui divise une chaîne en parties est souvent nécessaire lorsqu’on travaille avec des tableaux :

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

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

Traitement des numéros de téléphone :

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

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

Remplacement d’une sous-chaîne dans une chaîne :

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

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

</details>

<details>

<summary>Exemple de code à copier</summary>

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

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

/*diviser la chaîne en parties*/
elements = splitter('s, W, q', ',')
text='text1:text2:text3:text4:text5'
texts = splitter(text, ':',2)

/*title()*/
nom complet = John Smith
nom complet = title("#{nom complet}")

/*traitement du numéro de téléphone*/
phone = normalizePhone("+971 50 123 4567")

/*remplacement d’une sous-chaîne dans une chaîne*/
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/fr/chatbot/functions/calculatrice/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.
