# Formulas - String Updated by Sofia Teran

If is your first time with Formulas, please check our Getting Started guide here and our crash course here

• Capitalize
• Contains
• Length
• Lower
• Replace
• Slice
• Split
• Title
• ToString
• Upper
• Encode
• Concat

#### Capitalize

##### Capitalize text "hello landbot"

The first lowercase letter of the text will be converted to uppercase letter.

Let's see the result of the formula Capitalize in the text "hello landbot":

`Capitalize("hello landbot")`

#### Contains

##### Check if the user answered "landbot"

Let's take a look on this example:

We established a text block Question asking which is the client's favorite startup and he answered "landbot", then we configure the formula Contains, so if the user answered "landbot", it'll come out through the green line (true) and if he answered something else, it'll come out through the red line (false):

`Contains(@text,"landbot")`

#### Length

##### Check length of a text

We want to check if the text is too long:

`Length(@userinput)`
Keep in mind that the white spaces between words will also be counted!

In case we need to set a maximum, minimum or exact length for an answer, what we can do is to use the flow below, where we are going to check that the input of the user is no longer than 100 characters:

1. First, we ask the user for input, and store it in the variable @userinput
1. After that we will check the length of the @userinput, and store its value in the variable @userinputlength (Number format)
1. Once you have that value, we will use a Conditional block to check whether is that value greater or not than 100. Here is where we can set for any length that we want or condition that we want, like only 10 characters.
1. After that we set up 2 flows, one, through the Green Output (True) in case it goes according to your needs, and another flow, for the Red Output (False) when we want the user to type again something that matches your criteria, by using another Question block, with a different text, but using the same @userinput variable

#### Lower

##### Convert to lower case

Let's take a look in the next example, where email is our variable and we want to make sure is in lower case for validation.

`Lower(@email)`

#### Concatenate

Join together two strings, or a string and extra characters

`Concat("\$", " ", @amount)`

#### Replace

##### We replace the "a" of hahaha for "e"
`Replace(text, old, new)`
##### Mimimi, replace all vowels for an "i"
`Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(@toreplace, "a", "i"),"e","i"),"o","i"),"u","i"),"y","i"),"é","i"),"è","i"),"á","i"),"à","i"),"ò","i"),"ó","i"),"ù","i"),"ó","i"),"ü","i")`
##### Build a list out of an array (Web)

Let's imagine we are using Checkbox question type in the Multi-question block and we want to export the user's answer to the Google Sheet.

In this case, the user's answer will be saved in array format, as you can see in this screenshot:

In order to export to the Google Sheet this answer, which is saved in the variable @checkbox, we will remove square brackets " [ ] " and apostrophes " ' " with Replace formula.

To remove the " [ " from the answer, the formula to use would be:

`Replace(@checkbox,"[","")`

Please note that we put empty space in the new replacement (""), because what we want is to remove it.

Now, instead of creating three separate Formulas blocks, we will show you how you can combine multiple functions into the same formula:

In the formula we are removing " [ ", " ] " and " ' ":

`Replace(Replace(Replace(@checkbox,"[",""),"]",""),"'","")`

Here's the final result of the formula that is saved in the variable @checkbox_string and ready to be exported!

##### Build a list out of an array (in Whatsapp)

Let's say that we have an array coming from an external API, that we store in a Landbot variable type array called @array. With the following value:

["Dog","Cat","Rabbit","Mouse"]

Then using this formula, we will store the desired in list in the variable @list type string:

`Replace(Replace(Replace(Replace(@array,"[",""),"]",""),"'",""),", ","\n")`

So it will be displayed like this:

#### Slice

##### Extract part of the text

We want to extract some consecutive elements from our text "welcome to landbot":

`Slice(@text,1,9)`
##### Extract part of the text

We want to know the date of birth through an ID number (9402181234556):

1. Add the set variable and save the variable @ide in String format and type the ID number (9402181234556) in the value:
1. Then add the first Slice formula:

- Keep in mind that the position to start from, Counts from 0, and the position to end at, Counts from 0:

• 9 4 0 2 1 8 1 2 3 4 5 5 6

0 1 2 3 4 5 6

- Save the Output of in the variable @dateofbirth

1. Add a Message block and insert the Output variable @dateofbirth
1. Add the second Slice formula:

- Keep in mind that the position to start from, Counts from 0, and the position to end at, Counts from 0:

• 9 4 0 2 1 8 1 2 3 4 5 5 6

0 1 2

- Save the Output of in the variable @year

1. Add the fourth Slice formula:

- Keep in mind that the position to start from, Counts from 0, and the position to end at, Counts from 0:

• 9 4 0 2 1 8 1 2 3 4 5 5 6

0 1 2 3 4

- Save the Output of in the variable @month

1. Add the third Slice formula:

- Keep in mind that the position to start from, Counts from 0, and the position to end at, Counts from 0, this means 9 (0) 1 2 3 4 5 6 (8)

- Save the Output of in the variable @day

1. Add a message block with all the variables @year, @month, @day to confirm if the formula works right:
1. Final view in the chatbot:
Note: you can click in String > Slice to see the examples

#### Split

##### Get a list out of options selected in Multiple choice
`Split(@options,", ")`

Note, if you want to limit the number of options, this will be handy if you add use also the Length formula and with a Conditional block, you redirect the flow according to the maximum number of options you require. Like in the example below:

Where the formula is:

`Length(Split(@options, ","))`

#### Title

##### Capitalize all the words

Here's an example: we established a text block Question asking where is Spain and the first character in each word was converted to the capital (uppercase) letter and the remaining characters to lowercase.

`Title(@text)`

#### ToString

##### Transform number's " . " (dots) into " , " (commas)

We will need to use 2 formulas for this one:

1. First, we're using ToString to turn the number from the Number format into the String format
`ToString(@number)`

Make sure to save the result in the String format, and add the variable in the number format in the formula!

1. Another Formulas block to Replace the " . " (dots) for " , " (commas):

Save it also in the String format, and add the following formula to replace the dots for commas:

`Replace(@newstring, ".", ",")`

#### Upper

##### All content to Uppercase

We established a text block Question asking: "What would you write in the sand if you get lost in an Island? And the answer was "help" and our formula converted the text to all uppercase letters.

`Upper(@text)`

#### Encode

##### Prepare variable with spaces to use in url

`Encode(@name)`

### How did we do?

#### Formulas - Regex 