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
String.split in ComputerCraft?
Started by PixelFox, Jan 20 2015 09:56 PM
6 replies to this topic
#1
Posted 20 January 2015 - 09:56 PM
#2
Posted 20 January 2015 - 10:01 PM
Look at string.gmatch and patterns, you can accomplish this with those.
Sentance -> words
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,"
#3
Posted 20 January 2015 - 10:01 PM
I would suggesting looking at some of the implementations here.
The easiest is:
To understand this I would suggest looking at Lua patterns.
Edit: Beat me to it Yami!
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!
Edited by SquidDev, 20 January 2015 - 10:02 PM.
#4
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.
#5
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.
#6
Posted 20 January 2015 - 10:11 PM
Lyqyd, I don't know how to use gmatch. So it wouldn't help me.
#7
Posted 21 January 2015 - 08:56 AM
KingofGamesYami, on 20 January 2015 - 10:01 PM, said:
Look at string.gmatch and patterns, you can accomplish this with those.
Sentance -> words
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 by Dragon53535, 21 January 2015 - 08:56 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











