Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Splitting some text to a table, how could I do it?

Asked by 5 years ago
Edited 5 years ago

Hello! I'm trying to make a table that will contain nothing. I want to cut a piece of text, (that's not what I'm asking) and I want to split that string into a table. Example:

1table = {}
2split(table, text)
3print(table)
4-- prints: "t", "e", "x", "t"

Thank you so much for any little help!

1 answer

Log in to vote
0
Answered by 5 years ago

I can think of two ways to do this.

First, you can just add each character to a table with a simple loop using string.sub.

1local function Split(String)
2    local Table = {}
3    for I = 1, #String do
4        table.insert(Table, string.sub(String, I, I))
5    end
6    return Table
7end
8Split("Test") -- > {"T", "e", "s", "t"}

Alternatively you could use string.split.

1string.split("Test", "") -- > {"T", "e", "s", "t"}

Good luck!

0
it does ThatPreston 354 — 5y
Ad

Answer this question