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 4 years ago
Edited 4 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:

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

Thank you so much for any little help!

1 answer

Log in to vote
0
Answered by 4 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.

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

Alternatively you could use string.split.

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

Good luck!

0
it does ThatPreston 354 — 4y
Ad

Answer this question