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:
1 | table = { } |
2 | split(table, text) |
3 | print (table) |
4 | -- prints: "t", "e", "x", "t" |
Thank you so much for any little help!
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
.
1 | local 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 |
7 | end |
8 | Split( "Test" ) -- > {"T", "e", "s", "t"} |
Alternatively you could use string.split
.
1 | string.split( "Test" , "" ) -- > {"T", "e", "s", "t"} |
Good luck!