Table of Contents

Formulas - String

Judit Updated by Judit

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

Formulas available

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

Capitalize

You can capitalize the first letter of the text using formulas. See the example below:

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

The bot will recognize if the answer contains a particular value. See the example below:

Check if the user answered "landbot"

We established a Text Block Question asking: "What is your favorite start-up?" and the client answered "landbot". What we have to do is configure the formula Contains, so it will recognize the user answered "landbot", and it will come out through the green line (true). If the user had answered something else, it would have come out through the red line(false):

Contains(@text,"landbot")

Length

The bot can identify how long a text is. See the example below:

Check the length of a text

We want to check if the text is too long:

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

In case we need to set a maximum, minimum or exact length for an answer; we can use the flow below, where we are going to set up a Conditional Block where the input of the user is no longer than 100 characters and if the input is great we will send another Question block, with a different text, but using the same "@userinput" variable.

See the steps to follow below:

  1. First, we have to 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" (under number format)
  1. Once you have that value, we will use a Conditional Block to check whether the value is more or less than 100 characters. Here is where we can set any length that we want or condition that we want.
  1. After that we set up 2 flows, one, through the Green Output (True) in case the input has 100 or less characters and another flow through the Red Output (False) when the input is greater than 100 characters. In this case, we will send another Question block, with a different text, but using the same @userinput variable so that the user can type a shorter answer.

Lower

The bot can convert from uppercase to lowercase using formulas. See example below:

Convert to lower case

In this case, "email" is our variable and we want to make sure it is in lower case for it to be validated.

Lower(@email)

Replace

The bot can replace letters using formulas. See the examples below:

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 this answer to the Google Sheet, which is saved under 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 list in the variable "@list" type string:

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

So it will be displayed like this:

Slice

The bot will extract part of the text using formulas. See the examples below:

We want to extract some consecutive elements from our text "welcome to landbot":
Slice(@text,1,9)

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. Then type the ID number (9402181234556) in the value:
  1. Now 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

The bot can convert the first letter of words from lowercase to uppercase using formulas. See the example below:

We established a text block question asking where is Spain and the first character in each word was converted to capital letter (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

Convert all content to Uppercase

We established a Text block asking: "What would you write in the sand if you got lost in an Island? And the answer was "help". With the formula that we used below, the text was converted to uppercase.

Upper(@text)

Encode

Prepare variables with spaces to use in the URL
Encode(@name)

How did we do?

Formulas - Date

Formulas - Logical

Contact