Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is string.split and how do you use it?

In roblox what does string.split do? Please provide an example.

3 comments

0

are u asking about string.sub or string.split????

0

oops i meant string.split

0

it returns the first and second part of the string in a table

3 answers

✓ Accepted answer

Hello!

string.split is a function that gives parts of a string separated by the character/pattern you give it!

For example, let's say we had a string named "Hello Everyone!" to the variable String Now we are going to split the string by the separator or called delimiter.

local String = "Hello Everyone!"
string.split(String, " ")

Would this do anything? It wouldn't.

string.split returns a array, or a table with only numerical indexes.

-- Arrays

local Array = {
	1, -- If you dont provide a index, it'd default to 1!
	2, -- Index 2
	"Hello!" -- Index 3
}, or

local Array = {
	[1] = "Hello, this is the first index!"
}

-- This is NOT a array:

local Dictionary = {
	KeyOne = "ValueOne", -- Index "KeyOne" (Notice how its not a number?)
	KeyTwo = "ValueTwo"
}

-- You can access array's with number's: Array[3] would give: "Hello!"

So, we need to store the result.

local String = "Hello Everyone!"
local Result = string.split(String, " ")

for Index, Value in pairs(Result) do
	print(Index, Value)
end

In this code, we are looping over the result of string.split which is an array. This script would give us: 1: Hello 2: Everyone!

It would give us this, because we told the function to give us the characters split by " ". Which is a string.

As you can see, it separates characters by the second argument you give it.

string.split(string, character to split by)

Usages: Admin Commands, More advanced string manipulating. And much more!

String.split is a function that splits a string using a given string you want into a table of each string that was split

For example:

local splitContents = string.split("hey, guys, where, who, when", ",")

Gives you a table where "hey", "guys", "where", "who", "when" is found (whitespaces included aka space you see that are not letters/commas)

You can iterate through this table:

for index, value in ipairs(splitContents) do
	print(index, value)
end

This would print: 1 hey 2 guys 3 where 4 who 5 when

You could probably see why this is useful in admin commands for example.

2 comments

0

TL;DR: It allows you to separate or group pieces of a String via a given delimiter (or regular expressions—patterns) into an array; this code splits by commas, therefore each word found after a comma will be met at a different index.

0

That explanation is short but probably gonna be confusing to the asker, for example, what is a "given delimiter"? But, that should be how I explained it, just separated words into different indexes including whitespace.

string.split(s string, string pattern) returns a table. It is used to split strings, as you know from the name. For example:

local splitter = string.split("hi,,there", ",")

will return the table:

{"hi", "", "there"}

After you split it, you can use a for loop to print out all the values in the table.

for i,v in pairs(splitter) do
	print(v.." ["..i.."].")
end

I hope this helps!

~Dan_PanMan, advanced scripter.

This discussion is preserved as it was. New answers, comments, and votes are closed.