Poraric
Poraric

Reputation: 37

What is %s+ or %s used for in lua and how do you use it?

How do i use %s in lua, or a better question would be how is it used?

so here is what i have tried before assuming this is how it is used and how it works.

local arg1 = 'lmao'

print('fav string is %arg1')

at first i thought it was something used to reference a string or numeral inside of a string without doing like

print('hello '..name..'!')

Can someone provide me some examples or a explanation on how this is used and what for?

Upvotes: 1

Views: 4763

Answers (2)

glenn jackman
glenn jackman

Reputation: 247012

Lua uses %s in patterns (Lua's version of regular expressions) to mean "whitespace". %s+ means "one or more whitespace characters".

Ref: https://www.lua.org/manual/5.3/manual.html#6.4.1

Upvotes: 3

luther
luther

Reputation: 5564

A % in a string has no meaning in Lua syntax, but does mean something to certain functions in the string library.

In string.format, % is used to make a format specifier that converts another argument to a string. It's documented at string.format, but that refers to Output Conversion Syntax and Table of Output Conversions to explain almost all of the specifier syntax.

The % is also used to designate a character class in the pattern syntax used with some string functions.

Here is your code using string.format:

local arg1 = 'lmao'
print(string.format('fav string is %s', arg1))

Or, taking advantage of the string metatable:

local arg1 = 'lmao'
print(('fav string is %s'):format(arg1))

Upvotes: 5

Related Questions