Learn Lua · Strings · Lesson 3 of 3

Finding and changing text

Strings have functions you can call with a colon: name:upper() is the same as string.upper(name).

LUA
local code = "fly-a4"
code:upper()      --> "FLY-A4"
code:sub(1, 3)    --> "fly"

sub(from, to) takes characters by position, starting at 1. string.match finds text by a pattern: %d+ means one or more digits, and %a+ one or more letters:

LUA
string.match("SO-1042", "%d+")  --> "1042"
string.match("SO-1042", "%a+")  --> "SO"

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = { code = "fly-a4", reference = "SO-1", name = "Flyer" }

Steps

  1. Return code: input.code in capital letters.
  2. Add number: the digits from input.reference, such as "1042" from "SO-1042".
  3. Add short: the first three characters of input.name.
Show a hint

Each field is one call. A pattern describes the kind of characters you want, not the exact text, so it works for any reference.