How to split string into parts with specific amount?

How do I split a string into parts with specific amount of characters?

For example: If I’ve got the string “ExampleString” and split it by 5 it would print “Examp”, “leStr”, “ing”

Any help appreciated!

You can use string.sub to split it by 5 each time.

1 Like
local source = "ExampleString"

local text = string.sub(source,1,5)

print(text)

--Output: Examp

If you want to check it for every 5th segment, here:

local splitString = "ExampleString"

local SplitNumber = 5
local StartNum = 1

for i = 1, string.len(splitString) % SplitNumber do
	local endIndex = StartNum + SplitNumber - 1
	local result = string.sub(splitString, StartNum, endIndex)

	print(result)

	StartNum = endIndex + 1
end

This was exactly what I was looking for, thanks so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.