Vital.sandbox
String

string.rep

Shared

Repeats a string n times with optional separator


Syntax

local result = string.rep(input, count, separator = "")

Parameters

TypeNameDescription
stringinputString to be repeated
intcountNumber of times to repeat the string
stringseparatorSeparator to insert between repetitions

Returns

TypeNameDescription
stringresultString formed by repeating the input string specified number of times

Examples

Repeat a string multiple times
local result = string.rep("ab", 3)

engine.print("info", result) --'ababab'
Repeat a single character to form a line
local line = string.rep("-", 40)

engine.print("info", line) --'----------------------------------------'
Repeat a string with a separator
local result = string.rep("hi", 3, "-")

engine.print("info", result) --'hi-hi-hi'
Create a padding string
local padding = string.rep(" ", 10)

engine.print("info", "["..padding.."]") --'[          ]'
Return empty string with zero repetitions
local result = string.rep("test", 0)

engine.print("info", result) --''
Return the original string with one repetition
local result = string.rep("test", 1)

engine.print("info", result) --'test'
Repeat using method syntax
local result = string.rep("*", 5)

engine.print("info", result) --'*****'
Create a separator line
local separator = string.rep("=", 50)

engine.print("info", separator)
Build a pattern string
local dots = string.rep(".", 3)

engine.print("info", dots) --'...'
Create a visual progress bar
local bar = string.rep("█", 10)

engine.print("info", bar) --'██████████'
Repeat words with a space separator
local result = string.rep("word", 4, " ")

engine.print("info", result) --'word word word word'
Create indentation with repeated spaces
local indent = string.rep("  ", 3)

engine.print("info", indent.."code") --'      code'
Build an ASCII art pattern
local pattern = string.rep("* ", 5)

engine.print("info", pattern) --'* * * * * '
Create a repeated table row
local cells = string.rep("| Cell ", 4)

engine.print("info", cells.."|") --'| Cell | Cell | Cell | Cell |'
Generate a fixed-length test string
local data = string.rep("A", 100)

engine.print("info", #data) --100
Repeat a multi-character pattern
local pattern = string.rep("abc", 3)

engine.print("info", pattern) --'abcabcabc'
Repeat fields with a comma separator
local csv = string.rep("field", 5, ",")

engine.print("info", csv) --'field,field,field,field,field'

On this page