Strings
Strings in SamaLang are sequences of characters, identical to Lua strings.
Creating Strings
Section titled “Creating Strings”Strings are enclosed in double quotes:
ada sapa = "Halo, Dunia!"ada kosong = ""String Concatenation
Section titled “String Concatenation”Use .. to join strings:
ada nama = "Sama"ada versi = "Lang"ada gabung = nama .. versi
tulis(gabung) -- Output: SamaLangMix strings with numbers using tostring():
ada umur = 20ada pesan = "Umur saya " .. tostring(umur) .. " tahun"tulis(pesan)String Length
Section titled “String Length”Use # to get the length:
ada teks = "SamaLang"tulis(#teks) -- Output: 8Common String Functions
Section titled “Common String Functions”| 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" |
ada nama = "samalang"
tulis(string.upper(nama)) -- Output: SAMALANGtulis(string.sub(nama, 1, 4)) -- Output: samatulis(string.rep("ha", 3)) -- Output: hahahaEscape Sequences
Section titled “Escape Sequences”| Sequence | Description |
|---|---|
\n |
Newline |
\t |
Tab |
\\ |
Backslash |
\" |
Double quote |
tulis("Baris pertama\nBaris kedua")tulis("Tab\tdi sini")tulis("Quote: \"halo\"")Multi-line Strings
Section titled “Multi-line Strings”Use double square brackets for multi-line strings:
ada puisi = [[Budaya adalah akarBahasa adalah jiwaKode adalah masa depan]]
tulis(puisi)String Comparison
Section titled “String Comparison”Strings are compared lexicographically (alphabetically):
lamen "abc" < "abd" tres tulis("abc lebih kecil") -- This runsjure_mo
lamen "abc" == "abc" tres tulis("Sama!") -- This runsjure_moType Checking
Section titled “Type Checking”ada s = "SamaLang"tulis(type(s)) -- Output: stringtulis(type(42)) -- Output: number