Using String Format?

How are you suppose to use the first argument of

string.format()

?

2 Likes

The first argument to string.format is the “description” as described at the bottom of this article:

https://www.lua.org/pil/20.html

It is how you are formatting the string.

print(string.format(“pi = %.4f”, PI))
–> pi = 3.1416 d = 5; m = 11; y = 1990

print(string.format(“%02d/%02d/%04d”, d, m, y))
–> 05/11/1990 tag, title = “h1”, “a title”

print(string.format(“<%s>%s</%s>”, tag, title, tag))
–> <h1>a title</h1>

2 Likes

Is it the same as the string pattern for string.match?

This here holds what you need, for the most part. I learned this just yesterday actually and I’m pretty giddy about it. These patterns and whatnot are what you use in conjunction with various methods from the string library.

1 Like

image

you broke it!

4 Likes

n-no not me dont look at me

4 Likes

Page doesnt exist.

Got another source?

http://wiki-origin.roblox.com/index.php?title=String_pattern

what would in “”%2d:%02d"" mean in

string.format("%2d:%02d", minute, second)

then? I cant find that on the page

1 Like

FYI, these are called “format strings” and are 100% unrelated to Lua’s variation of regex.

https://www.google.com/search?q=c+format+string

string.format just delegates to C’s sprintf, so you can look at C documentation/tutorials.

@Fm_Trick’s link doesn’t point to the right topic.

Edit:

Is it the same as the string pattern for string.match?

To reiterate, absolutely not. You will just confuse yourself if you try to relate them. They have loose similarities, in that they use the % character a lot, but not much beyond that.

The code examples i posted were from the link I posted, at the bottom.

Ah, yes, missed that you posted twice. The first link does go to a relevant page. This second one (http://wiki-origin.roblox.com/index.php?title=String_pattern) is not related to string.format at all.

http://wiki-origin.roblox.com/index.php?title=Format_string
http://www.cplusplus.com/reference/cstdio/printf/

2 Likes

Hold on, you were experiencing that problem too? I thought @Fm_Trick was just joking. My page is fine.

Probably just another Developer Hub problem, as usual.