115 posts
Posted 20 January 2015 - 09:56 PM
Is there a way to split lines in a sentence or words in a line into a table?
e.x
local Sentence = "Hello, I like things."
local SentenceSplit = {String.Split(Sentence)}
print(SentenceSplitz[])
print(SentenceSplit[2])
- Thanks, Lightning
3057 posts
Location
United States of America
Posted 20 January 2015 - 10:01 PM
Look at string.gmatch and patterns, you can accomplish this with those.
Sentance -> words
local s = "Hello, I like things." )
local words = {}
for word in s:gmatch( "%S+" ) do
words[ #words + 1 ] = word
end
print( words[ 1 ] ) --#should print "Hello,"
1426 posts
Location
Does anyone put something serious here?
Posted 20 January 2015 - 10:01 PM
I would suggesting looking at some of the implementations
here.
The easiest is:
function string.split(str, sep)
return {str:match((str:gsub("[^"..sep.."]*?"..sep, "([^"..sep.."]*?)"..sep)))}
end
To understand this I would suggest looking at Lua patterns.
Edit: Beat me to it Yami! :ph34r:/>
Edited on 20 January 2015 - 09:02 PM
115 posts
Posted 20 January 2015 - 10:07 PM
Squid Dev, You're Idea is much better IMO, It's a function and can be changed, And it's just Simple.
8543 posts
Posted 20 January 2015 - 10:09 PM
Uh, you could easily make a function out of the gmatch method, and it's a significantly better way to do it.
115 posts
Posted 20 January 2015 - 10:11 PM
Lyqyd, I don't know how to use gmatch. So it wouldn't help me.
1080 posts
Location
In the Matrix
Posted 21 January 2015 - 08:56 AM
Look at string.gmatch and patterns, you can accomplish this with those.
Sentance -> words
local s = "Hello, I like things." )
local words = {}
for word in s:gmatch( "%S+" ) do
words[ #words + 1 ] = word
end
print( words[ 1 ] ) --#should print "Hello,"
local function splitString(str)
local words = {}
for word in str:gmatch( "%S+" ) do
words[ #words + 1 ] = word
end
return words
end
*GASP* It's suddenly a function
Edited on 21 January 2015 - 07:56 AM