HomeIndex

Strings

stringreplace(string, find, replace)
Returns a copy of string with find replaced with replace
strstartswith(string, head)
Returns true if string starts with head
strendswith(string, tail)
Returns true if string ends with tail
strfirst(string)
Returns the first character of a string
Character is returned as a string.
s = "abc" print(strfirst(s)) // "a"
strlast(string)
Returns the last character of a string
Character is returned as a string.
s = "abc" print(strfirst(s)) // "c"
strfind(string, needle)
Returns zero based index of needle in string
If the needle can't be found, it returns null
strfindi(string, needle)
Returns zero based index of case insensitive needle in string
If the needle can't be found, it returns null
strcount(string, needle)
Returns the number of times needle is found in string.
s = "bob bob bob tony bob" print(strcount(s, "bob")) // 4
strslice(string, start, end)
Returns a string sliced from index start to index end inclusivly.
If end is omitted, the string from start to the end of the string is returned.
If end is before start a empty string is returned. If start or end is negative, the index is from the end of the string.
local s = "0123456789" print(strslice(s, 0, 5)) // "012345" print(strslice(s, 6)) // "6789" print(strslice(s, 4, 4)) // "4" print(strslice(s, -4, -3)) // "67" print(strslice(s, -5)) // "56789" print(strslice(s, 0, -2)) // "012345678"
strlen(string)
Returns the length of the string
This is really the number of bytes of the string and will fail with utf-8 strings contaning non-ascii.
strlower(string)
Returns a new string in all lowercase.
s = "ABcdEF" print(strlower(s)) // "abcdef"
strupper(string)
Returns a new string in all uppercase.
s = "ABcdEF" print(strlower(s)) // "ABCDEF"
strhash(string)
Returns 32-bit hash of the string
local s = "Thimbleweed Park" print(strhash(s)) // -853823676
strsplit(string, marker)
Splits a string into an array of strings
The marker need to be a single character string
s = "first-second-third" a = strsplit(s, "-") print(a[0]) // "first" print(a[1]) // "second" print(a[2]) // "third" s = "first--third" a = strsplit(s, "-") print(a[0]) // "first" print(a[1]) // "" print(a[2]) // "third"
strseperate(string, markers)
Returns an array of strings seperated by any characters in markers
local s = "abc---def--===---ghi=-=-=-=jkl------" local a = strseperate(s, "-=") foreach (s in a) { print(s) // prints "abc", then "def", "ghi", and "jkl" }
strjoin(array, glue)
Returns a string of an array of strings joined with the glue
local s = strjoin(["rita", "betsy", "mary"], ",") print(s) // "rita,betsy,mary"
strlines(string)
Returns an array of strings seperated by cr or lf.
Useful for reading a file from disk and breaking it into lines.
strguid(type, length=32)
Returns a random GUID of type and length
the type can be one of the following:
1 = Alpha (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
2 = Digits (0123456789)
3 = Alpha+digits (ABCDEF0123456789)
4 = Safe alpha (ABCDEFGHKMNPQRSTUVWXYZ)
5 = Safe alpha+digits (ABCDEFGHKMNPQRSTUVWXYZ23456789)
6 = Hex digits (ABCDEF0123456789)
7 = GUID (012345678-9ABC-DEF0-1234-56789ABCDEF0)
8 = MongoID (0123456789abcdef012345678)
9 = Hex lowercase digits (0123456789abcdef)
s = strguid(1) // "AKUJEDFFSFPCYPHBJXDGCLKGTTTXTEXM" s = strguid(2) // "06424169963749469047716156517578" s = strguid(3) // "39SAJVDH36KXEL2NEHXL469LJVMS2BQY" s = strguid(4) // "CKAEPKBAGYGQDXBBUREURGXZWMSPERRW" s = strguid(5) // "DXEBBXZP8HYVYAR647P4EGEZHUQ4K73B" s = strguid(6) // "101B6A16BD70211E9ED162600F050856" s = strguid(7) // "5148013B-A2D2-044F-9B63-1763932CDE15" s = strguid(8) // "69b411d3fdc4faa3958bc8bc" s = strguid(9) // "e2393f86e98537f8f660b270b6f2c29b" s = strguid(1, 5) // "XJUGH"
format(format, ...)
Returns a formatted string
local s = format("%d", 100)
s is now 100
local s = format("%s", "Ester")
s is now Ester
local s = format("%s is %d years old", "Ester", 89)
s is now Ester is 89 years old
The format function should not be used in time-critical situations.
compilestring(string)
Compiles a string into a callable function.
local f = compilestring("return 100") n = f() // n is 100