String help plz

So I am doing some stuff and what I need to do is add a string to a string after a certain point, example:

local stringToAdd = [[
Line1 Text
Line2 Text
]]

local ex = [[
Name
Title
Desc

Cheese
]]

so what I would want to do is add stringToAdd to ex, but what I want to do, is add it right after Desc, but before Cheese, now lets say ex was this:

local ex = [[
Desc
Name
Title
Desc

Cheese

]]

Now in this case there are two Desc, I would like to add the string after the last Desc in the String. So uh how can I do this? Thanks!

This is hella confusing to read. Are you talking about ..?

local RandomString = "hi,"
local extraString = "I have achieved godhood"

print(RandomString.. extraString) -- This is one way you could do this.
print(RandomString, extraString) -- Second way you could do this.

This works, but again, I need to put the string in between Desc and Cheese
(Sorry for my silly names lol)

Idk what you mean, honestly. Do you mean this?

local stringToAdd = {Line1 = "Hi", Line2 = "She likes whatever she likes bc I don't know!")
local ex = {Name = "FroDev", Title = "Pro Scripter", Desc = "She is a girl guys", Cheese = "idk what to put here"}

print(ex.Name, ex.Desc, stringToAdd.Line2, ex.Cheese)

Ok uh so the example I showed you wasnt a table, its a string on multiple lines, which is created to my memory via the brackets ( [[ ]] ), so yeah

I’m trying my best to comprehend what you want here, but I’m not grasping what it is. Could you try to explain just a liiittle bit better?

1 Like

I need to wedge a string between two lines of a string. I dont know how much better I can explain it, im not good at this lol.

local string = [[
Line1
-- I nned to slap a string right here in between the two lines
Line2

]]

So a string in a string?

ExtraText

I didn’t even know you could make strings like this. I will see what I can do for you.

@sonic_848 Basically.

1 Like

Okay, so this is probably stupidly inefficient, but this is what I found.

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

print(string.sub(string.gsub(Message, “placeholder”, “I am Frodev”), 1, 28))

1 Like

Hmm this actually might work. Ill see what I can do

If you want to put an indent in a string you can just do \n

"hello \n world!"
text : 
"hello 
 world!"

that does work, but when writing it out it helps being able to visualize it more

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