Section Strings
String utility functions
Summary
Constant | Type | Summary |
---|---|---|
UNICODE_DELTA | string | Unicode delta: U+0394 = Δ |
UNICODE_DOUBLE_FLAT | string | Unicode double flat: U+1D12B = 𝄫 |
UNICODE_DOUBLE_SHARP | string | Unicode double sharp: U+1D12A = 𝄪 |
UNICODE_FLAT | string | Unicode flat: U+266D = ♭ |
UNICODE_NATURAL | string | Unicode natural: U+266E = ♮ |
UNICODE_SHARP | string | Unicode sharp: U+266F = ♯ |
UNICODE_SLASHED_O | string | Unicode slashed-O: U+00F8 = ø |
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. |
bool | IsNumeric(string str) Is str representing a number? |
bool | IsStyledString(string str) Test if a string is a |
table | SplitUrl(string url) Split an URL into protocol, host, path, query and anchor. |
string | excel_column_name(int n) Returns the Excel column name of the n-th column. |
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. |
table | string_split(string s, string delim) 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. |
string | string_time(number seconds, exact (default=false), bool 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. |
Constants
string UNICODE_DOUBLE_FLAT
Unicode double flat: U+1D12B = 𝄫
string UNICODE_DOUBLE_SHARP
Unicode double sharp: U+1D12A = 𝄪
Functions
IsStyledString(string str)
Test if a string is a
Parameter | Type | Default | Description |
---|---|---|---|
str | string |
- Return
- bool: true or nil (for false).
IsNumeric(string str)
Is str representing a number?
Parameter | Type | Default | Description |
---|---|---|---|
str | string | 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.
Parameter | Type | Default | Description |
---|---|---|---|
str | string |
- Return
- string: % are escaped by a %.
string_split(string s, string delim)
Split the string s by a string delimiter delim.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | The string to split | |
delim | string | 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.
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_join(table list, string delimiter)
Concat the elements of table list, separated by the string delimiter.
Parameter | Type | Default | Description |
---|---|---|---|
list | table | ||
delimiter | string |
- 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.
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"
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 |
- 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.
Parameter | Type | Default | Description |
---|---|---|---|
seconds | number | 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). | |
clockFormat | bool | false | Clock 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.
Parameter | Type | Default | Description |
---|---|---|---|
a | string | ||
b | string |
- 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.
Parameter | Type | Default | Description |
---|---|---|---|
a | string | ||
b | string |
- Return
- 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 |
- 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