parse_newlines is a function that will simply turn newlines created by the Enter/return key to “\n”.
"Hello,
World!"
-- becomes
"Hello,\nWorld!"
split_newlines is a function that will turn a string into an array by newlines “\n”:
"Hello,\nWorld!"
-- becomes
{"Hello,", "World!"}
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.
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)
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
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!