Skip to content

Strings

Strings in SamaLang are sequences of characters, identical to Lua strings.

Strings are enclosed in double quotes:

Terminal window
ada sapa = "Halo, Dunia!"
ada kosong = ""

Use .. to join strings:

Terminal window
ada nama = "Sama"
ada versi = "Lang"
ada gabung = nama .. versi
tulis(gabung) -- Output: SamaLang

Mix strings with numbers using tostring():

Terminal window
ada umur = 20
ada pesan = "Umur saya " .. tostring(umur) .. " tahun"
tulis(pesan)

Use # to get the length:

Terminal window
ada teks = "SamaLang"
tulis(#teks) -- Output: 8
Function Description Example
string.len(s) Get length string.len("halo")4
string.upper(s) Convert to uppercase string.upper("halo")"HALO"
string.lower(s) Convert to lowercase string.lower("HALO")"halo"
string.sub(s, i, j) Extract substring string.sub("halo", 1, 2)"ha"
string.find(s, pattern) Find pattern string.find("halo", "lo")3, 4
string.rep(s, n) Repeat string string.rep("ha", 3)"hahaha"
string.reverse(s) Reverse string string.reverse("halo")"olah"
string.format(fmt, ...) Format string string.format("Umur: %d", 20)
string.gsub(s, pat, rep) Replace pattern string.gsub("halo", "l", "r")"haro"
Terminal window
ada nama = "samalang"
tulis(string.upper(nama)) -- Output: SAMALANG
tulis(string.sub(nama, 1, 4)) -- Output: sama
tulis(string.rep("ha", 3)) -- Output: hahaha
Sequence Description
\n Newline
\t Tab
\\ Backslash
\" Double quote
Terminal window
tulis("Baris pertama\nBaris kedua")
tulis("Tab\tdi sini")
tulis("Quote: \"halo\"")

Use double square brackets for multi-line strings:

Terminal window
ada puisi = [[
Budaya adalah akar
Bahasa adalah jiwa
Kode adalah masa depan
]]
tulis(puisi)

Strings are compared lexicographically (alphabetically):

Terminal window
lamen "abc" < "abd" tres
tulis("abc lebih kecil") -- This runs
jure_mo
lamen "abc" == "abc" tres
tulis("Sama!") -- This runs
jure_mo
Terminal window
ada s = "SamaLang"
tulis(type(s)) -- Output: string
tulis(type(42)) -- Output: number