File SMStrings.mys
Essential string utility functions
SMStrings.mys is automatically included when you Include "SMUtils"
.
- Sylvain Machefert
Sections
Section | Summary |
---|---|
Durations | Clock and durations utility functions
|
Files | Files utility functions |
Fonts | Font utility functions. |
Internet | Internet functions. |
Strings | strings utility functions |
Tables | tables utility functions. |
Summary
Return type | Function and summary |
---|---|
string | EscapePercent(string str) Escape a string that contain a % that can be interpreted in Lua string functions like gsub. |
boolean | IsNumeric(string str) Is str representing a number? |
boolean | IsStyledString(string str) Test if a string is a StyledString |
RemoveDiacritics(string s) Remove diacritics (accents) from the string s. | |
string, string | SplitPath(string path) Split a path into directory and file, no file if path ends by a '/'. |
table | SplitUrl(string url) Split an URL into protocol, host, path, query and anchor. |
string | escape_lines(string s) Escape lines \n and \r as well as tabs \t and backslash \, using backslashes \. |
string | excel_column_name(int n) Returns the Excel column name of the n-th column. |
string | gsub_args(string str, ...) Replace %1, %2 in str by first and second following arguments. |
string | string_between(string str, string left, string right) Return the portion of str found between left and right. |
string | string_concat(string src, string delimiter, ...) Concatenate toAdd after src with a delimiter, handles nil and empty string initialization. |
boolean | string_ends_with(string str, string rightStr) Does str ends with rightStr? |
string | string_first(string a, string b) Returns the first of a and b in alphabetical order. |
string | string_join(table list, string delimiter) Concat the elements of table list, separated by the string delimiter. |
string | string_last(string a, string b) Returns the last of a and b in alphabetical order. |
string | string_left(string str, int n) Return the n leftmost chars of the string str. |
string | string_line_to_space(string str) Replace line \r and \n characters by a space. |
string | string_right(string str, int n) Return the n rightmost chars of the string str. |
table | string_slice(string s, int sze, boolean fromEnd) Slice the string into blocks of size, except last one that may be shorter. |
table | string_split(string s, string delim, int limit) Split the string s by a string delimiter delim. |
table | string_split_lines(string s) Split the string s by lines, handle \r, \n and \r\n. |
boolean | string_starts_with(string str, string leftStr) Does str starts with leftStr? |
string | string_time(number seconds, boolean exact, boolean clockFormat) Converts a time (music duration, debug chrono...) into readable string. |
string | string_trim(string s, string toTrim, string side) Removes toTrim at the beginning and the end of s. |
string | unescape_lines(string s) Unescape lines \n and \r as well as tabs \t and backslash \, escaped by backslashes. |
Functions
IsStyledString(string str)
Test if a string is a StyledString
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str |
- Return
- boolean:
true
ornil
(for false).
IsNumeric(string str)
Is str representing a number?
Parameter | Type | Default | Description |
---|---|---|---|
str | string | The string to test |
- Return
- boolean:
true
ornil
(for false) - Example
IsNumeric("-22.5") --> true IsNumeric("-.5") --> true IsNumeric(".12") --> true IsNumeric("0.12") --> true IsNumeric("1.") --> false IsNumeric("123abc") --> false
EscapePercent(string str)
Escape a string that contain a % that can be interpreted in Lua string functions like gsub.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str |
- Return
- string: % are escaped by a %.
string_split(string s, string delim, int limit)
Split the string s by a string delimiter delim.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | The string to split | |
delim | string | The delimiter | |
limit | int | 0 | Maximum number of slices of string, values <= 1 are ignored.. |
- Return
- table: An array of at least one item (s if delim is not found)
- Example
local t = string_split("Hello World!", " ") print(t[1]) --> Hello print(t[2]) --> World!
string_split_lines(string s)
Split the string s by lines, handle \r, \n and \r\n.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | The string to split |
- Return
- table: A table of at least one element (s if line separator is not found)
string_slice(string s, int sze, boolean fromEnd)
Slice the string into blocks of size, except last one that may be shorter.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | The string to slice | |
sze | int | Size of each slice | |
fromEnd | boolean | (defaul=false) Slice from the end of the string, it means that, when there are multiple slices, if fromEnd == true, first slice's length may be smaller than size, if fromEnd == false last slice's length may be smaller than size. |
- Return
- table: A table of slices of s
- Error
- if s is not a string, or size is not a positive number
string_join(table list, string delimiter)
Concat the elements of table list, separated by the string delimiter.
Parameter | Type | Default | Description |
---|---|---|---|
list | table | list | |
delimiter | string | delimiter |
- Return
- string: string
- Example
string_join({"Anna", "Bob", "Charlie", "Dolores"}, ", ") --> Anna, Bob, Charlie, Dolores
string_concat(string src, string delimiter, ...)
Concatenate toAdd after src with a delimiter, handles nil
and empty string initialization.
- s is not concatenated if empty,
- delimiter is not inserted if src is
nil
or empty.
myList = string_concat(myList, ", ", "an item")
is a a shortcut for myList = (myList and (myList..", ") or "") .. "an item"
with more nil
handling.Parameter | Type | Default | Description |
---|---|---|---|
src | string | src | |
delimiter | string | "" | |
... | List of string or number. |
- Return
- string: string
- Error
- if s is
nil
string_trim(string s, string toTrim, string side)
Removes toTrim at the beginning and the end of s.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | If nil , returns "" | |
toTrim | string | " \t\r\n" | List of chars to remove, if nil, then trim spaces, tabs, lines. |
side | string | "both" | Side of the trim, "left", "right" or anything else for both sides. |
- Return
- string: The trimmed string
- Example
string_trim(" abcd\t")) --> "abcd" string_trim(" abcd\t", nil, "left") --> "abcd\t" string_trim(" abcd\t", "a \t") --> "bcd" string_trim(" abcd\t", "()") --> " abcd\t"
string_left(string str, int n)
Return the n leftmost chars of the string str.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str | |
n | int | n |
- Return
- string: A string of
strlen(str)
to n chars:strsub(str, 1, n)
- Error
- if str is not a string or
n <= 0
string_right(string str, int n)
Return the n rightmost chars of the string str.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str | |
n | int | n |
- Return
- string: A string of
strlen(str)
to n chars:strsub(str, -n)
- Error
- if str is not a string or
n <= 0
string_starts_with(string str, string leftStr)
Does str starts with leftStr?
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str | |
leftStr | string | leftStr |
- Return
- boolean:
true
ifstring_left(str, strlen(leftStr)) == leftStr
,nil
for false. - Error
- if str is not a string, or
strlen(leftStr) == 0
string_ends_with(string str, string rightStr)
Does str ends with rightStr?
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str | |
rightStr | string | rightStr |
- Return
- boolean:
true
ifstring_right(str, strlen(rightStr)) == rightStr
,nil
for false. - Error
- if str is not a string, or
strlen(rightStr) == 0
string_between(string str, string left, string right)
Return the portion of str found between left and right.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str | |
left | string | left | |
right | string | right |
- Return
- string:
nil
if left or right are not found in str.
string_line_to_space(string str)
Replace line \r and \n characters by a space.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str |
- Return
- string: string
excel_column_name(int n)
Returns the Excel column name of the n-th column.
"A" for 1, "B" for 2, "Z" for 26, "AA" for 27...
Parameter | Type | Default | Description |
---|---|---|---|
n | int | n |
- Return
- string: string
- Example
excel_column_name(123) --> "DS"
string_time(number seconds, boolean exact, boolean clockFormat)
Converts a time (music duration, debug chrono...) into readable string.
Parameter | Type | Default | Description |
---|---|---|---|
seconds | number | Time in seconds, 0.010 = 10ms | |
exact | boolean | false | If false, removes details (e.g. no milliseconds if time is minutes and you don't need so much precision). |
clockFormat | boolean | false | Clock format: returns 00:03.010 instead of 3s 10ms |
- Return
- string: "10 ms", "3 s 456 ms", "1 min 14 s", 00:03.010...
- See
- Stopwatch:new
- Example
s = 1.0102345 -- 1 second, 10,2345 ms print(string_time(s)) --> "1.01 s" print(string_time(s, true)) --> "1 s 10.234 ms" print(string_time(s, false, true)) --> "00:01.010" print(string_time(s, true, true)) --> "00:01.010234" s = s + 2*60 -- add 2 minutes print(string_time(s)) --> "2 min 1 s" print(string_time(s, true)) --> "2 min 1 s 10.234 ms" print(string_time(s, false, true)) --> "02:01.010" print(string_time(s, true, true)) --> "02:01.010234"
string_first(string a, string b)
Returns the first of a and b in alphabetical order.
Comparable to max(a,b)
for numbers.
Parameter | Type | Default | Description |
---|---|---|---|
a | string | a | |
b | string | b |
- Return
- string: string
- Example
print(string_first("Hello", "Harmony")) --> "Harmony"
string_last(string a, string b)
Returns the last of a and b in alphabetical order.
Comparable to max(a,b)
for numbers.
Parameter | Type | Default | Description |
---|---|---|---|
a | string | a | |
b | string | b |
- Return
- string: string
- Example
print(string_last("Harmony", "Assistant")) --> "Harmony"
SplitUrl(string url)
Split an URL into protocol, host, path, query and anchor.
Parameter | Type | Default | Description |
---|---|---|---|
url | string | url |
- Return
- table: Table t with following structure for https://www.myriad-online.com/cgi-bin/bbs/YaBB.pl?board=MYRSCRIPT;action=display;num=1652718688#2
t.Protocol = "https" t.Host = "www.myriad-online.com" t.Path = "cgi-bin/bbs/YaBB.pl" t.Query = "board=MYRSCRIPT;action=display;num=1652718688" t.Anchor = "2"
Some of these field may be empty string ornil
- Error
- if url is
nil
or empty string
SplitPath(string path)
Split a path into directory and file, no file if path ends by a '/'.
Parameter | Type | Default | Description |
---|---|---|---|
path | string | path |
- Returns
- string: directory
- string: file name
RemoveDiacritics(string s)
Remove diacritics (accents) from the string s.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | s |
- Return
- The string without diacritics, e.g. 'รก' is replaced by 'a'.
- Error
- if s is not a string
- See
- https://en.wikipedia.org/wiki/Diacritic
escape_lines(string s)
Escape lines \n and \r as well as tabs \t and backslash \, using backslashes \.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | A string that may contain CR and/or LF |
- Return
- string: escaped string.
- See
- unescape_lines
- Example
s = "string\twith\nnew line" print(s) --> string with --> new line print(escape_lines(s)) --> string\twith\nnew line
unescape_lines(string s)
Unescape lines \n and \r as well as tabs \t and backslash \, escaped by backslashes.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | A escaped string |
- Return
- string: unescaped string ready to be printed.
- See
- escape_lines
- Example
s = "string\\twith\\nnew line" print(s) --> string\twith\nnew line print(unescape_lines(s)) --> string with --> new line
gsub_args(string str, ...)
Replace %1, %2 in str by first and second following arguments.
This is a convenient way to write the following code:
return gsub(gsub(str, "%%1", "1st value"), "%%2", "2nd value")
gsub
return 2 value, the string and number of substitutions. Strangely, nested gsub
accept the second returned value, but if you write gsub(str, 1, "%%2", "2nd value")
you get an error.
But the second returned value may disturb your functions!
This variant return only the modified string, it may save you debugging headaches ;)
Parameter | Type | Default | Description |
---|---|---|---|
str | string | The string containing %1, %2... to be replaced | |
... | Undefined number of arguments that must be string, number or nil . |
- Return
- string: The replaced string
- Error
- If one argument has unsupported type.
- Example
local s = "Hello %1, I want %2." print(gsub_args(s, "Bob", "bananas")) --> Hello Bob, I want bananas.