Section Strings

String utility functions

Summary

ConstantTypeSummary
UNICODE_DELTAstringUnicode delta: U+0394 = Δ
UNICODE_DOUBLE_FLATstringUnicode double flat: U+1D12B = 𝄫
UNICODE_DOUBLE_SHARPstringUnicode double sharp: U+1D12A = 𝄪
UNICODE_FLATstringUnicode flat: U+266D =
UNICODE_NATURALstringUnicode natural: U+266E =
UNICODE_SHARPstringUnicode sharp: U+266F =
UNICODE_SLASHED_OstringUnicode slashed-O: U+00F8 = ø
Return typeFunction and summary
stringEscapePercent(string str)
Escape a string that contain a % that can be interpreted in Lua string functions like gsub.
boolIsNumeric(string str)
Is str representing a number?
boolIsStyledString(string str)
Test if a string is a StyledString
tableSplitUrl(string url)
Split an URL into protocol, host, path, query and anchor.
stringexcel_column_name(int n)
Returns the Excel column name of the n-th column.
stringstring_first(string a, string b)
Returns the first of a and b in alphabetical order.
stringstring_join(table list, string delimiter)
Concat the elements of table list, separated by the string delimiter.
stringstring_last(string a, string b)
Returns the last of a and b in alphabetical order.
tablestring_split(string s, string delim)
Split the string s by a string delimiter delim.
tablestring_split_lines(string s)
Split the string s by lines, handle \r, \n and \r\n.
stringstring_time(number seconds, exact (default=false), bool clockFormat)
Converts a time (music duration, debug chrono...) into readable string.
stringstring_trim(string s, string toTrim, string side)
Removes toTrim at the beginning and the end of s.

Constants

string UNICODE_FLAT

Unicode flat: U+266D =

See
https://www.compart.com/fr/unicode/U 266D

string UNICODE_NATURAL

Unicode natural: U+266E =

See
https://www.compart.com/fr/unicode/U 266E

string UNICODE_SHARP

Unicode sharp: U+266F =

See
https://www.compart.com/fr/unicode/U 266F

string UNICODE_DOUBLE_FLAT

Unicode double flat: U+1D12B = 𝄫

See
https://www.compart.com/en/unicode/U 1D12B

string UNICODE_DOUBLE_SHARP

Unicode double sharp: U+1D12A = 𝄪

See
https://www.compart.com/en/unicode/U 1D12A

string UNICODE_DELTA

Unicode delta: U+0394 = Δ

See
https://www.compart.com/fr/unicode/U 0394

string UNICODE_SLASHED_O

Unicode slashed-O: U+00F8 = ø

See
https://www.compart.com/fr/unicode/U 00F8

Functions

IsStyledString(string str)

Test if a string is a StyledString

ParameterTypeDefaultDescription
strstring  
Return
bool: true or nil (for false).

IsNumeric(string str)

Is str representing a number?

ParameterTypeDefaultDescription
strstring The string to test
Return
bool: true or nil (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.

ParameterTypeDefaultDescription
strstring  
Return
string: % are escaped by a %.

string_split(string s, string delim)

Split the string s by a string delimiter delim.

ParameterTypeDefaultDescription
sstring The string to split
delimstring The delimiter
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.

ParameterTypeDefaultDescription
sstring The string to split
Return
table: A table of at least one element (s if line separator is not found)

string_join(table list, string delimiter)

Concat the elements of table list, separated by the string delimiter.

ParameterTypeDefaultDescription
listtable  
delimiterstring  
Return
string:
Example
string_join({"Anna", "Bob", "Charlie", "Dolores"}, ", ")
--> Anna, Bob, Charlie, Dolores

string_trim(string s, string toTrim, string side)

Removes toTrim at the beginning and the end of s.

ParameterTypeDefaultDescription
sstring If nil, returns ""
toTrimstring" \t\r\n"List of chars to remove, if nil, then trim spaces, tabs, lines.
sidestring"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"

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...

ParameterTypeDefaultDescription
nint  
Return
string:
Example
excel_column_name(123) --> "DS"

string_time(number seconds, exact (default=false), bool clockFormat)

Converts a time (music duration, debug chrono...) into readable string.

ParameterTypeDefaultDescription
secondsnumber Time in seconds, 0.010 = 10ms
(default=false)exact If false, removes details (e.g. no milliseconds if time is minutes and you don't need so much precision).
clockFormatboolfalseClock format: returns 00:03.010 instead of 3s 10ms
Return
string: "10ms", "3s 456ms", "1min 14s"
Example
= 0.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"
printEquals(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.

ParameterTypeDefaultDescription
astring  
bstring  
Return
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.

ParameterTypeDefaultDescription
astring  
bstring  
Return
string:
Example
print(string_last("Harmony", "Assistant")) --> "Harmony"

SplitUrl(string url)

Split an URL into protocol, host, path, query and anchor.

ParameterTypeDefaultDescription
urlstring  
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 "" or nil
Error
if url is nil or an empty string