Modifying Strings with Lua

How you can use the Lua scripting language to modify string text fields in your form

Angela Lim avatar
Written by Angela Lim
Updated over a week ago

Strings are text fields that can contain letters and numbers, and we typically reference between double quotation marks " " .

In VerticalChange, you can take strings and modify them, then put the result of the modification into a Single Line Text Field. 

Below are examples of ways you can modify text strings in your form. There are more options available to you than what is shown below, so if you are interested in more, check out the Lua guide on Strings

Concatenating Two Strings Together

If you would like to combine two strings together into one, you can do so using the concatenation syntax - ..

For example:
answers.full_name = contact.first_name .. " " .. contact.last_name 

Tip: The .. " " ..  in between the First Name and Last Name adds a space in between the two strings!

Updating a string to all lowercase/uppercase

answers.lowercase_name = string.lower(contact.first_name .. " " .. contact.last_name) 

 Replace string.lower  with string.upper  if you would like the text field to be all uppercase

Removing all spaces, numbers and punctuations from a string

If you would like to remove certain characters from a string (like punctuation), you can do so using the following syntax:

string.gsub(answers.string_to_use, pattern, replace_bad_characters_with) 

..where answers.string_to_use is the text field, pattern is a regular expression that indicates which characters in that string to keep,  and replace_bad_characters_with is what character(s) to replace any unwanted/bad characters with

For example:
answers.cleaned_up_name = string.gsub(answers.full_name, "[^a-zA-Z]", "") 

 This is looking at a field with the slug full_name . Using Regular Expression formatting (regex), it is indicating that it wants to keep any characters that are lowercase or uppercase letters [^a-zA-Z] . It is then indicating that anything that is not a lowercase or uppercase letter, replace with an empty field "" .

If answers.full_name  was "John O'Reilly Jr.", answers.cleaned_up_name would be "John OReilly Jr".

Check out the following tutorials on regular expression to learn more about how to write the patterns you need
https://regexone.com/
​ https://regex101.com/

Returns a substring of the string passed

If you would like to grab part of a string to be used in another field, you can use the following syntax:

string.sub(answers.string, i [, j]) 

..where answers.string is the string you want to modify, i is the position in the string where it should start to evaluate, and j is an optional argument that says where to end the substring. If j is left blank, it will evaluate until the end of the string

For example:

answers.shortened_string = string.sub("Hello Lua user", 7)
=> answers.shortened_string = “Lua user”
answers.shortened_string = string.sub("Hello Lua user", 7, 9)
=> answers.shortened_string = “Lua”

Did this answer your question?