UTF8
February 16, 2015 ยท View on GitHub
- utf8
- startwith
- endswith
- iequals
- equals
- sub
- char
- len
- indexof
- lastindexof
- replace
- contains
- trim
- tolower
- toupper
- empty
- split
- join (table)
- join (...)
utf8
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"
utf8.startswith( string, find )
Checks if string start with the text find.
print(utf8.startswith("foo bar", "foo")); -- true
print(utf8.startswith("foo bar", "bar")); -- false
utf8.endswith( string, find )
Checks if string ends with the text find.
print(utf8.endswith("foo bar", "foo")); -- false
print(utf8.endswith("foo bar", "bar")); -- true
utf8.iequals( a, b )
Performs a case-insensitive equality check.
print(utf8.iequals("foo", "FOO")); -- true
print(utf8.iequals("foo", "foo")); -- true
utf8.equals( a, b )
Performs a case-sensitive equality check.
print(utf8.equals("foo", "FOO")); -- false
print(utf8.equals("foo", "foo")); -- true
utf8.sub( string, start, [length] )
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"
utf8.char( string, index )
Returns the character at a specific index in the string
print(utf8.char("This is a text string", 2)); -- 'i'
utf8.len( string )
Returns the length of a string.
print(utf8.len("This is a text string")); -- 21
utf8.indexof( string, find )
Returns the first index of find in the string.
print(utf8.indexof("This is a text string", "text")); -- 11
utf8.lastindexof( string, find )
Returns the last index of find in the string.
print(utf8.lastindexof("This is a text string", "t")); -- 17
utf8.replace( string, old, new )
Replaces the string old with the string new in the string.
print(utf8.replace("foo bar", "foo", "hello")); -- "hello bar"
utf8.contains( string, find )
Returns true if string contains the string find.
print(utf8.contains("foo bar", "bar")); -- true
print(utf8.contains("foo bar", "xyz")); -- false
utf8.trim( string )
Removes spaces from the start and end of the string.
print(utf8.trim(" foo ")); -- "foo"
utf8.tolower( string )
Returns a transformed version of the string where all letters are in lower-case
print(utf8.tolower("FOO")); -- "foo"
utf8.toupper( string )
Returns a transformed version of the string where all letters are in upper-case
print(utf8.toupper("foo")); -- "FOO"
utf8.empty( string )
Checkes if the string is empty.
print(utf8.empty("foo")); -- false
print(utf8.empty("")); -- true
print(utf8.empty(" ")); -- false
utf8.split( string, delim )
Splits the subject string using the specified delim.
print(utf8.split("foo bar hello world", " ")); -- { "foo", "bar", "hello", "world" }
utf8.join( delim, table )
Joins the items in table using delim.
local items = { "foo", "bar" };
print(utf8.join(" ", items)); -- "foo bar";
utf8.join( delim, part1, part2, ... )
Joins all parts using delim.
print(utf8.join(" ", "foo", "bar")); -- "foo bar";