exString - Various string functions
Posted: Thu Sep 16, 2021 12:35 am
Hi. Lately i've been turning various functions that i keep writing from scratch when i need them into libraries. This is one of them.
exString is a collection of string related functions.
It's more properly documented on github, But here are some basics.
Import it:
Optionally you can
to append all the exString functions to the string table. I hear this is kinda frowned upon, But if you don't do this, You'll have to call the functions like
instead of just
and you wont be able to chain the functions like
It has the following functions:
exString.startsWith(str, start) / exString.endsWith(str, end)
These return true if 'str' starts/ends with 'start'/'end'
exString.append(str, delimiter, ...)
This appends all arguments after the 'delimiter' to str then returns it. All arguments are tostring'd.
If delimiter is not false/nil, The appended strings will be seperated by it
exString.split(str, delimiter)
This one splits a string by 'delimiter', And collects the segments in a table. Then returns it. 'delimiter' defaults to ",". It doesn't seem to like it when you use "$" as a delimiter. I'm working on that.
exString.strip(str)
This one's inspired by python. It strips all leading & trailing whitespace & punctuation from str and returns it.
exString:replace(str, target, repl)
Yeah i'll be straight, This is just an alias for string.gsub. Even i don't use this one.
exString:print(str)
This is just an alias for print, But if you imported exString, You can call it directly on strings which i find neat
Hope someone besides myself finds this useful. <3
exString is a collection of string related functions.
It's more properly documented on github, But here are some basics.
Import it:
Code: Select all
exString = require("exString")
Code: Select all
exString.import()
Code: Select all
str = exString.strip(str)
Code: Select all
str = str:strip()
Code: Select all
str = str:replace("something", "something else"):strip()
exString.startsWith(str, start) / exString.endsWith(str, end)
These return true if 'str' starts/ends with 'start'/'end'
exString.append(str, delimiter, ...)
This appends all arguments after the 'delimiter' to str then returns it. All arguments are tostring'd.
If delimiter is not false/nil, The appended strings will be seperated by it
Code: Select all
local str = "Hello"
str:append(false, " world", "!"):print()
> "Hello world!"
This one splits a string by 'delimiter', And collects the segments in a table. Then returns it. 'delimiter' defaults to ",". It doesn't seem to like it when you use "$" as a delimiter. I'm working on that.
Code: Select all
local str = "comma,seperated,string"
spl = str:split()
> {"comma", "seperated", "string"}
This one's inspired by python. It strips all leading & trailing whitespace & punctuation from str and returns it.
Code: Select all
local str = " hello!"
str:strip():print()
> "hello"
Yeah i'll be straight, This is just an alias for string.gsub. Even i don't use this one.
exString:print(str)
This is just an alias for print, But if you imported exString, You can call it directly on strings which i find neat
Code: Select all
str = "Hello world!"
str:print()
> "Hello world!"