Help with a custom format function

Hey devforum! I’ve been stuck on this for quite a minute now. I was writing a library for manipulating markdown and needed a quick and simple way to parse the markdown.

I ended up with just deciding to write my own format function.

Here’s the code:

local function join(t: {string})
    local s = ""
    
    for i,v in t do
        s = s..v
    end

    return s
end

-- // The point of this function is to allow easy parsing of markdown.
-- // The first arg is a string and you can think of it to be like a format string.
-- // The second arg is a string to format it with.
-- // The idea behind this is we compare formatWith to str and replace what's different in str with what's in formatWith.
local function format(str: string, formatWith: string)
    local readableStr = str:split("")
    local readableFormatWith = formatWith:split("")

    for i,v in readableFormatWith do
        if readableStr[i] == v then
            readableFormatWith[i] = ""
        end
    end

    formatWith = join(readableFormatWith)
    return string.format(str, formatWith)
end

print(format("*%q*", "*hi*"))
Output: *"hi"*
Expected Output: *hi*

It’s not an issue with the join function as I have tested it prior.

Any help is much appreciated and thank you in advance!

I believe the issue is that you are using %q.
From the string library documentation:

The reason that you are having the quotation marks added is because it is making the string be a in a form that can be safely read back by the interpreter.

I would recommend you use %s (which outputs the string of the value) in place of %q.

thank you very much. it was originally intentional, but I guess I’ll just work around it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.