Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Which is better to use, string.format or concatenation?

Asked by
Validark 1580 Snack Break Moderation Voter
9 years ago

In this case, I'm assuming that both can be used easily.

There are some cases, such as the following, where string.format can come in handy, because of it's ability to use to to make sure that the script puts in a 0 in the tens digit if the seconds are only at 8 or something. Are there any other cases or things string.format is really good for? Which is faster?

local function secondsToStamp(timeInSeconds) -- Converts 65 seconds to 01:05, see Lua string Library for more information
    return string.format("%d:d", math.floor(timeInSeconds / 60), timeInSeconds % 60)
end

This function is the same, but a little bit messier:

local function secondsToStamp(timeInSeconds) -- Converts 65 seconds to 01:05, see Lua string Library for more information
    return math.floor(timeInSeconds / 60) .. ":" .. (timeInSeconds % 60 >= 10 and "" or "0") .. (timeInSeconds % 60) 
end

And even worse:

-- This one asks for a 2 digits at the beginning, which I decided I do not want for my newer script, but did not edit this because reasons
function secondsToStamp(seconds) -- Converts 65 seconds to 01:05
    local mins = tostring(math.floor(seconds / 60)) -- seconds / 60 rounded down
    local secs = tostring(math.floor(seconds % 60)) -- Remainder of seconds / 60
    return (#mins < 2 and string.rep("0", 2 - #mins) or "") .. mins .. ":" .. (#secs < 2 and string.rep("0", 2 - #secs) or "") .. secs -- Return our string, while filling in the blanks so its 01:05 instead of 1:5
end

What are the other advantages of using string.format? Is there much of a difference?

2 answers

Log in to vote
4
Answered by 9 years ago

String.gsub is also a useful function worth considering, it can replace any found parts of a string with a new part just like string.format does. E.g. if you wanted to create a time formatter, you could do something like this

local Format = "HH:MM"
local hour = 14
local minute = 18
Format = string.gsub(Format,"HH",hour):gsub("MM",minute) --> 14:18

Otherwise string.format would be a much more advanced way of doing the task compared to concatenation, so utilising it would be much more efficient and professional :)

0
Thanks, but I still think string.format is a better alternative in this case. Validark 1580 — 9y
0
I like this, haven't thought of it before. It's nice because it gives names to each of the fields BlueTaslem 18071 — 9y
0
Yes, but if minutes was under 10, then you would need to add on an additional 0. Validark 1580 — 9y
Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
8 years ago
Edited 8 years ago

The fastest version of this function would look like this:

local format = string.format

local function SecondsToStamp(Seconds)
    -- Converts 65 seconds to 01:05
    return format("%d:%.2d", Seconds / 60, Seconds % 60)
end

Answer this question