- utf8
- startwith
- endswith
- iequals
- equals
- sub
- char
- len
- indexof
- lastindexof
- replace
- contains
- trim
- tolower
- toupper
- empty
- split
- join (table)
- join (...)
The utf8
library provides essential string manipulation functions for UTF8 encoded text.
local utf8 = require("utf8");
All functions can be used as global functions
print(utf8.replace("foo bar", "foo", "hello")); -- "hello bar"
For best performance with large strings, create a UTF8 string instance and re-use it when possible. Note the instance (str:
) syntax!
local str = utf8.new("foo bar");
str:replace("foo", "hello");
print(str:raw()); -- "hello bar"
Checks if string
start with the text find
.
print(utf8.startswith("foo bar", "foo")); -- true
print(utf8.startswith("foo bar", "bar")); -- false
Checks if string
ends with the text find
.
print(utf8.endswith("foo bar", "foo")); -- false
print(utf8.endswith("foo bar", "bar")); -- true
Performs a case-insensitive equality check.
print(utf8.iequals("foo", "FOO")); -- true
print(utf8.iequals("foo", "foo")); -- true
Performs a case-sensitive equality check.
print(utf8.equals("foo", "FOO")); -- false
print(utf8.equals("foo", "foo")); -- true
Gets a part of a string
from the start
position to the end of the string or if length
is set to the set length.
print(utf8.sub("This is a text string", 2, 5); -- "his i"
print(utf8.sub("This is a text string", 2)); -- "his is a text string"
Returns the character at a specific index
in the string
print(utf8.char("This is a text string", 2)); -- 'i'
Returns the length of a string
.
print(utf8.len("This is a text string")); -- 21
Returns the first index of find
in the string
.
print(utf8.indexof("This is a text string", "text")); -- 11
Returns the last index of find
in the string
.
print(utf8.lastindexof("This is a text string", "t")); -- 17
Replaces the string old
with the string new
in the string
.
print(utf8.replace("foo bar", "foo", "hello")); -- "hello bar"
Returns true if string
contains the string find
.
print(utf8.contains("foo bar", "bar")); -- true
print(utf8.contains("foo bar", "xyz")); -- false
Removes spaces from the start and end of the string
.
print(utf8.trim(" foo ")); -- "foo"
Returns a transformed version of the string
where all letters are in lower-case
print(utf8.tolower("FOO")); -- "foo"
Returns a transformed version of the string
where all letters are in upper-case
print(utf8.toupper("foo")); -- "FOO"
Checkes if the string
is empty.
print(utf8.empty("foo")); -- false
print(utf8.empty("")); -- true
print(utf8.empty(" ")); -- false
Splits the subject
string using the specified delim
.
print(utf8.split("foo bar hello world", " ")); -- { "foo", "bar", "hello", "world" }
Joins the items in table
using delim
.
local items = { "foo", "bar" };
print(utf8.join(" ", items)); -- "foo bar";
Joins all parts using delim
.
print(utf8.join(" ", "foo", "bar")); -- "foo bar";