exString - Various string functions

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
veethree
Inner party member
Posts: 874
Joined: Sat Dec 10, 2011 7:18 pm

exString - Various string functions

Post by veethree »

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:

Code: Select all

exString = require("exString")
Optionally you can

Code: Select all

exString.import()
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

Code: Select all

str = exString.strip(str)
instead of just

Code: Select all

str = str:strip()
and you wont be able to chain the functions like

Code: Select all

str = str:replace("something", "something else"):strip()
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

Code: Select all

local str = "Hello"
str:append(false, " world", "!"):print()
> "Hello world!"
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.

Code: Select all

local str = "comma,seperated,string"
spl = str:split()
> {"comma", "seperated", "string"}
exString.strip(str)
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"
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

Code: Select all

str = "Hello world!"
str:print()
> "Hello world!"
Hope someone besides myself finds this useful. <3
Attachments
exString.lua
(3.23 KiB) Downloaded 200 times
User avatar
dusoft
Party member
Posts: 482
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: exString - Various string functions

Post by dusoft »

veethree wrote: Thu Sep 16, 2021 12:35 am exString.strip(str)
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"
Good library. I have a comment on naming.

Strip should be IMHO called trim. Also, adding an optional function parameter would allow to trim other chars than ASCII 32 (space).
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests