String help plz

It’s quite simple if you break it down:

  1. parse_newlines is a function that will simply turn newlines created by the Enter/return key to “\n”.
"Hello,
World!"
-- becomes
"Hello,\nWorld!"
  1. split_newlines is a function that will turn a string into an array by newlines “\n”:
"Hello,\nWorld!"
-- becomes
{"Hello,", "World!"}
  1. insertStringAtLine simply inserts the given string into the line with the option to add newline padding and converts it back into a string.
insertStringAtLine([[Hello,
World!]], "!!!", 2, false, true)
-- becomes
[[Hello,
!!!

World!]]

-- notice how there is no padding above but padding below
-- because I set the arguments to false and true respectively.
1 Like

Ok yall on the subject of strings, new problem while I was about to make this work I ran into a problem with gsub, heres the code:

local CommandToReplace = string.split(BaseCommandCode,'\n')
CommandToReplace[1] = string.gsub(CommandToReplace[1],'Command','test')

The error is:

invalid argument #1 to 'gsub' (string expected, got table)

So I printed what CommandToReplace[1] is, its a string, so like wth is going on lol

This is an almost swapped case I believe. You’re overwriting it at the same time of reading it. Try assigning it to a variable first

1 Like

Actually it’s a table I’m just dumb, it doesn’t have an almost swapped issue

1 Like

I don’t know exactly what’s going on in this case? I tried causing this to happen without intentionally passing CommandToReplace to string.gsub but even if there’s zero matches, empty split, etc. It will still be treated as {string} and assume an empty string.

This worked, of course I don’t know if this is the exact use case you were attempting, but this functions fine. I tried it too without a loop and only overriding index 1 but that has no issues either. Of course, if this is what you’re trying to do you should probably do the operation before splitting the string

local commands = [[
HelpCommand\n
PingCommand\n
NotACmd
]]

local splitCommands = commands:split("\n")
for index, command in splitCommands do
	splitCommands[index] = splitCommands[index]:gsub("Command", "test")
end

local mergedCommands = table.concat(splitCommands, "\n")
print(mergedCommands)
1 Like
local stringToAdd = [[
Line1 Text
Line2 Text
]]

local ex = [[
Name
Title
Desc

Cheese
]]

local result = ex:gsub("(.+Desc)(.?)", "%1" .. stringToAdd .. "%2")

print(result)

Change “%1” to “%1\n” if you want stringToAdd to start on a new line

Yeah you we’re right, gusb worked perfectly! Thanks!

1 Like

Hey can you try my solution and see if it works for you?

uh why? yours looks so much more complicated then what I did so Idk if thats necessary

1 Like

Mine inserts the string into the old one instead of just replacing a part of the string completely

True, but replacing it works just fine as I can just leave like a placeholder inside of the string, which again just kinda makes it easier so yeah.

Thanks though!

1 Like

Okay, well a simpler way of doing the solution is just to do

local Message = [[
Hi.
placeholder
I am a girl.
]]

print(Message:gsub("placeholder", "I am Frodev"))

Yeah, but they gave the general idea first so I marked it as the solution

1 Like

Over complication isn’t real it can’t hurt you

enjoy some unnecessarily bloated code, just for you frodev! just like the minecraft movie, we only throw money at Jack Black. sorry, a simple solution was just too much of an ask. we have simple solution at home!

local iAmString = {}
iAmString.__index = iAmString

function iAmString:__tostring()
     return table.concat(self.parts, self.joins)
end

function iAmString.new(start: string | {string}, ...: string)
    local self = setmetatable({}, iAmString)
    self.joins = " "

    if type(start) == "string" then
        self.parts = {start}
    else
        self.parts = start
    end

    local varargs = {...}
    if varargs ~= nil then
        for _, str in varargs do
            table.insert(self.parts, str)
        end
    end

    return self
end

function iAmString:find(str: string): number?
    return table.find(self.parts, str)
end

function iAmString:addLine(str: string, pos: number?)
    local length = #self.parts
    pos = pos or length + 1

    if pos <= length then
        local shifted = self.parts
        shifted[pos] = str

        for index = pos, length do
            shifted[index + 1] = self.parts[index]
        end

        self.parts = shifted
    else
        self.parts[pos] = str
    end

    return self
end

function iAmString:removeLine(pos: number?)
    pos = pos or 1
    table.remove(self.parts, pos)
    return self
end

function iAmString:setLine(str: string, pos: number | string)
    if type(pos) == "string" then
        pos = self:find(pos)
        assert(pos, "Could not find any matches")
    end

    self.parts[pos :: number] = str
    return self
end

local realString = iAmString.new("Steve")
realString.joins = "...\n"
realString
    :addLine("I", 1)
    :addLine("An", 2)
    :setLine("Steve!", 3)
    :setLine("Am", "An")

print(realString)
-- I...\n
-- Am...\n
-- Steve!

Thank you
Good night
This is my goodbye, the Piglins are getting close!

1 Like