-
Notifications
You must be signed in to change notification settings - Fork 11
Operators
Michael Ebens edited this page Jan 28, 2016
·
4 revisions
Strong adds a few handy operators to strings. These are listed below.
Some people like to use +
instead of ..
to concatenate strings, and that's what this operator is about.
Example
"Hello " + target
"Foo" + "Bar" -- "FooBar"
This operator allows you to take anything matching the specified pattern out of a string. This makes s - p
the same as s:gsub(p, '')
.
Example
"com.novafusion.nothing.here" - "^%w%.%w%.?" -- "nothing.here"
"Aliens!!!!!!" - "!+$" -- "Aliens"
This is the most useful in my opinion, it allows you to repeat a string a certain number of times. This makes s * i
the same as s:rep(i)
.
Example
"Hello... " * 3 -- "Hello... Hello... Hello... "
("Boo " * 3) - " $" -- "Boo Boo Boo"
See the split
method over at the function reference. Note that when using the division operator the plain
option is always set to true.
Does simple string interpolation by calling string.format
. Works with a single value or a table of values.
"Hello %s" % "World" -- "Hello World"
"%s for all, all for %d" % {"One", 1} -- "One for all, all for 1"