views:

2706

answers:

7

I need to do a simple split of a string, but there doesnt seem to be a function for this, and the manual way i tested didn't seem to work. How would i do it?

+5  A: 

Please see Splitting Strings:

Here are various ways of splitting a string into a list of substrings, breaking the original string on occurrences of some separator (character, character set, or pattern). This is commonly called a string split[2] function.

Andrew Hare
+3  A: 

If you are splitting a string in Lua, you should try the string.gmatch() or string.sub() methods. Use the string.sub() method if you know the index you wish to split the string at, or use the string.gmatch() if you will parse the string to find the location to split the string at.

Example using string.gmatch() from Lua 5.1 Reference Manual:

 t = {}
 s = "from=world, to=Lua"
 for k, v in string.gmatch(s, "(%w+)=(%w+)") do
   t[k] = v
 end
gwell
I "borrowed" an implementation from that lua-users page thanks anyway
RCIX
+1  A: 

Here is The Function
====================================================
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end

===============================================
Call it like,
list=split(string-to-split,pattern-to-match)
e.g.
list=split("1:2:3:4","\:")
===============================================
For more go here,
http://lua-users.org/wiki/SplitJoin

Faisal Hanif
+1  A: 

Just as string.gmatch will find patterns in a string, this function will find the things between patterns:

function string:split(pat)
  pat = pat or '%s+'
  local st, g = 1, self:gmatch("()("..pat..")")
  local function getter(segs, seps, sep, cap1, ...)
    st = sep and seps + #sep
    return self:sub(segs, (seps or 0) - 1), cap1 or sep, ...
  end
  return function() if st then return getter(st, g()) end end
end

By default it returns whatever is separated by whitespace.

Norman Ramsey
+1. Note to any other Lua beginners: this returns an iterator, and 'between patterns' includes the beginning and end of the string. (As a newbie I had to try it to figure these things out.)
Darius Bacon
A: 

im having an issue in lua for psp...

Im sending a string over ADHOC mode and i cant split the string on the other side...

script A will send the string like so Adhoc.send("hell0 /n date:")

And script B would spilt the string but idk how...

my friend said this:

data = Adhoc.recv()

lines = split(data) --you need to write the 'split' function yourself

screen:print(0, 0, data[1], green) --"hello" screen:print(0, 0, data[2], green) --"7/09/10"

so like how would i split this ????

Trey777
Welcome to stack overflow! You can ask a question via the Ask Question link in the top right, right now you've answered my question with yours and that's not how the system works.
RCIX
A: 

I would avoid Lua. The documentation is non-existent.

Lots of responders pointed to:

http://lua-users.org/wiki/SplitJoin

but of course, nothing on that page actually works in Lua:

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> split("123", "")
stdin:1: attempt to call global 'split' (a nil value)
stack traceback:
 stdin:1: in main chunk
 [C]: ?
> 

Looking at the non-searchable content at lua.org doesn't turn up much on the standard library (I assume there is one).

If something as simple as splitting a string can't be adequately documented, well, I wouldn't expect anything complicated to be achievable without a lot of pain.

Cliff Wells
Too bad you didn't realize those functions listed are not part of the stock lua distribution. You're supposed to copy that code as part of whatever program that needs it. Moreover, i can search lua.org just fine with google. The manual exists at http://www.lua.org/manual/5.1/manual.html and the entire standard library is well documented.
RCIX
You are right. I guess it didn't occur to me that the "well documented standard library" would exist as a footnote at the bottom of the page documenting the language proper.
Cliff Wells
When you miss the entire point of a language's existence, you really should keep your yap shut talking about it.
JUST MY correct OPINION
A: 

If you just want to iterate over the tokens, this is pretty neat:

line = "one, two and 3!"

for token in string.gmatch(line, "[^%s]+") do
   print(token)
end

Output:

one,

two

and

3!

Short explanation: the "[^%s]+" pattern matches to every non-empty string in between space characters.

Hugo