String help plz

You can still do something like this just put \n on where you want the indent, right?

[[
hello\n
world
]]

uh theres no point in putting \n in this example, cause like um yeah

i always just do

local extratext = "aaa"
text = `Line1{extratext}Line2`

but whatever works

1 Like

Use string.format with the %s pattern.

local Input = "cheese"
local Result = string.format("I like to eat %s pizza.", Input)

This is actually what string interpolation does internally.

I’m actually stumped. I don’t know if what they want is even possible. :grimacing:

1 Like

This is a very niche functionality that it’s likely better to opt for string patterns or arrays instead. Regardless, here is a solution I came up with.

Creating new lines with the Return/Enter key is kind of weird, a problem I solved in this workaround:

Applying that workaround, I split the message by new lines, inserted a new string at a line number, and turned it back into a string. Here is an example you can run in the console:

--!strict

local message = [[Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur at dignissim magna, et gravida nisi.
Ut nec ornare dolor. Nam dolor nisi, venenatis sit amet porta eu, egestas eu orci.
Phasellus faucibus fermentum nibh a tempus.
Pellentesque quis porttitor nulla.
Morbi malesuada quam quis dolor lacinia luctus.
Quisque pulvinar imperdiet arcu ac egestas. Ut quis aliquet massa.
Nullam sed leo tortor.

Praesent ultricies vestibulum nisl, ut interdum justo mollis non.
In semper justo eu imperdiet vehicula.
Quisque porta, nibh quis lobortis feugiat, libero lectus porta nibh, in fermentum felis arcu sit amet metus.
Aliquam pharetra lorem vitae vulputate pretium. Sed congue rhoncus metus sit amet efficitur.
Quisque in varius magna, at sollicitudin justo. Quisque mollis sagittis scelerisque.
Integer tempus vehicula justo. Aenean malesuada maximus nibh feugiat tristique.
Donec sed iaculis mauris. Suspendisse sodales nibh non mi pharetra varius.]]

-- parses newlines created by the Enter/Return key into "\n"
local function parse_newlines(str: string): string
	return string.gsub(str, "\n", "\\n")
end

-- splits string by "\n"
local function split_newlines(str: string): {string}
	return string.split(parse_newlines(str), "\\n")
end

-- inserts string into another string with newlines with optional padding
local function insertStringAtLine(original: string, add: string, at: number, upPadding: boolean?, downPadding: boolean?): string
	local lines: {string} = split_newlines(original)
	at = math.clamp(at, 1, #lines)
	
	if upPadding == true then
		table.insert(lines, at, "")
		at += 1
	end
	
	table.insert(lines, at, add)
	
	if downPadding == true then
		at += 1
		table.insert(lines, at, "")
	end
	
	local newString: string = ""
	
	for _: number, line: string in lines do
		if newString ~= "" then
			newString ..= "\n"
		end
		
		if line ~= "" then
			newString ..= line
		end
	end
	
	return newString
end

local message = insertStringAtLine(message, "Hello, World!", 9, true, false)
-- this will print properly in the console
print(message)

-- this will translate nicely into text objects
local screenGui = Instance.new("ScreenGui", game:GetService("StarterGui"))
local textBox = Instance.new("TextBox", screenGui)
textBox.Text = message
textBox.ClearTextOnFocus = false
textBox.Size = UDim2.fromScale(0.5, 1)

After examining all of this and determining that I can’t understand a single bit of it, I now ask myself why I wanted to learn how to script. :smiling_face_with_tear:

1 Like

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