Splitting a string at multiple different points

gonna explain with example

string = "Yoo I Just :oof: ed :bruh: !"
split = {"Yoo I Just ", " ed "," !"}

I also want the script to know that after the first one comes :oof: after second comes :bruh: e.t.c.

Use string.split():

local coolString = "Yoo I Just :oof: ed :bruh: !"
print(string.split(coolString, ":"))

The output for this will be:

{
  [1] = "Yoo I Just ",
  [2] = "oof",
  [3] = " ed ",
  [4] = "bruh",
  [5] = " !"
}
4 Likes

why th did i not know that? well thanks!

No problem, have a good day. If you have anything else to ask, feel free.

1 Like

I was using split btw. But didnt got a idea on how to do that simple thing… lol

local coolString = "Yoo I Just :oof: ed :bruh: !"
local splitString = string.split(coolString, ":")
print({splitString[1], splitString[3], splitString[4]})

mhm yes I know. And it works!! thanks!

1 Like

it has 1 flaw… It splits strings that I dont want splitted… for example

smile = "Hi! :)"
split = {"Hi! ",")"}

How would i tackle this? I want to only split text that are like

":oof:" -- I have a array for them.
local images = {
	[1] = {
		id = 1702642218,
		name = ":oof:"
	},
	[2] = {
		id = 8582339223,
		name = ":bruh:"
	},
	[3] = {
		id = 8518597017,
		name = ":cat:"
	},
	[4] = {
		id = 8918926280,
		name = ":keel:"
	}
}

There’s no simple way to do it with string patterns as string.split has yet to feature splitting with a pattern, You can try something like this perhaps if you want to split by things in a table

for _, v in ipairs(images) do
	str = str:gsub(v.name, "")
end

local splits = str:split("  ")

Where images is y our table, made this and used your table as a reference to get it working

What this does is that it’ll go through everything in the table, and removing instances of them by finding them with gsub and then replacing them with an empty string "".

Then at the end we split using two spaces as the separator (Because when we substitute with an empty string the two spaces inbetween it will merge) to get what you require

Example, Gave it “Hello :oof: there :keel: :)”

image

The only caveat is if there’s no spaces at the front and back of the things you want to split with as it’ll combine the strings before and after together into one element

Edit: Tried out @Jaycbee05’s solution and it looks to be working how you wanted it to given how you also want the spaces as well, and theirs is simpler, best to give them the solution since it’s a simpler way to resolve your problem, mostly cause I forgot %b existed haha

1 Like

Another potential solution is to use something like the null byte ("\0" or perhaps “:\0:”) to signify where you want a division to occur:

local str = "Yoo I Just :oof: ed :bruh: !"
local res = str:gsub("%b::", "\0"):split("\0") 

Note: if you only want to split exact keywords you can provide the replacement parameter of the gsub with a table:

local marker = "\0"

local replace = {
	[":oof:"] = marker,
	[":bruh:"] = marker
}

local res = str:gsub("%b::", replace):split(marker)
2 Likes

Both answers of @EmbatTheHybrid and @Jaycbee05 are VERY good. I will be using them soon as I right now cant. If you want to test you can have this

local CustomEmoji = {}

local label

local images = {
	[1] = {
		id = 1702642218,
		name = ":oof:"
	},
	[2] = {
		id = 8582339223,
		name = ":bruh:"
	},
	[3] = {
		id = 8518597017,
		name = ":cat:"
	},
	[4] = {
		id = 8918926280,
		name = ":keel:"
	}
}

local num = 0

local function addlabel(text,frame)
	if text ~= "" then
		local line = Instance.new("TextLabel",frame)
		line.RichText = true
		line.Name = num
		num += 1
		line.TextSize = label.TextSize
		line.TextColor3 = label.TextColor3
		line.BackgroundTransparency = 1
		line.AutomaticSize = Enum.AutomaticSize.XY
		line.Text = text
	end
end

local function addimg(id,frame)
	local img = Instance.new("ImageLabel")
	img.Name = num
	num += 1
	img.Size = UDim2.fromOffset(label.TextSize,label.TextSize)
	img.Image = "rbxassetid://"..id
	img.BackgroundTransparency = 1
	img.Parent = frame
end

local function update(str)
	if label:FindFirstChild("Line") then label.Line:Destroy() end
	num = 0
	local split = string.split(str,":")

	local frame = Instance.new("Frame",label)
	frame.Name = "Line"
	frame.AutomaticSize = Enum.AutomaticSize.XY
	frame.BackgroundTransparency = 1
	frame.Size = UDim2.new(0,0,0,0)
	local uilistlayout = Instance.new("UIListLayout",frame)
	uilistlayout.FillDirection = Enum.FillDirection.Horizontal
	uilistlayout.SortOrder = Enum.SortOrder.Name

	for i,v in pairs(split) do
		local imgg = nil
		for _,img in pairs(images) do
			if v == string.gsub(img.name,":","") then
				imgg = img.id
				break
			end
		end
		if imgg then
			addimg(imgg,frame)
		else
			addlabel(v,frame)
		end
	end
end

function CustomEmoji.Update(Label,Text)
	label = Label
	update(Text)
end

return CustomEmoji

Thank you for our answers. And have a fantastic Day/Evening/Night!

sorry to say but @Jaycbee05 and @EmbatTheHybrid your methods werent that good like thats now what I wanted it removed the emoji name as well! which I wanted to see. @Katrist post is still the solution. What I now did is to remove those “:” of the emojis with a star icon “★” as most people dont do unicode in roblox so that works. Still thanks for the help!