typestar

Strings in Lua

Concatenate with .., format with string.format, # measures.

-- .. concatenates; string.format does the printf work.
local relic = "Moon Sigil"
local weight = 2.75

print("Found: " .. relic)
print(string.format("%s weighs %.1f lb", relic, weight))

-- The string library doubles as methods via the colon.
print(relic:upper())
print(relic:sub(1, 4))
print(("x"):rep(12))
print("Length: " .. #relic)

How it works

  1. .. joins; numbers coerce into the join.
  2. The colon calls string methods: relic:upper(), :sub(1, 4).
  3. #relic is the length operator, on strings and arrays alike.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
336
Tokens
67
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 57 seconds.

Type this snippet

Step 3 of 3 in Values, step 3 of 27 in Language basics.

← Previous Next →

Strings in other languages