Search code examples
variablesluarepeat

How do I build a variable from a character repeated N number of times, in Lua?


I have an input value of N.

I want to turn that into a variable consisting of N characters.

for example, if N = 12, and I'm repeating the character "H", the value of the created variable should look like this: "HHHHHHHHHHHH"

I need it to be a variable, because I intend to use it in a few other places.

I am completely new to Lua, by the way. I just started a few days ago.


Solution

  • You are looking for string.rep.

    For example:

    local result = string.rep("H", 12)
    print(result) -- prints "HHHHHHHHHHHH"