Jump to content




Split string(with a designated way to separete)

lua help api

  • You cannot reply to this topic
6 replies to this topic

#1 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 23 June 2015 - 05:17 PM

I want to split strings , but not with the default paterns like this:

for token in gmatch("lalla", [^%p]+) do
or with %a %c %d %l %w etc...

i want to do that but with a designated patern like this:
(not a real code , you know)

for token in gmatch("la:la\la.la", "\")

then the results will be : la:la and la.la

(sorry for any English error)

#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 23 June 2015 - 05:26 PM

What exactly do you mean?

#3 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 23 June 2015 - 05:32 PM

anything like split string with my , i choice , the term of the separation.

without use a patern like separete with ponctuation by %p

#4 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 23 June 2015 - 05:34 PM

ok, here it goes:

local function split(str,sep)
  sep = sep or " "
  local ret = {}
  for token in str:gmatch("[^"..sep.."]+") do
	 ret[#ret+1] = token
  end
return ret end

Edited by Creator, 23 June 2015 - 05:35 PM.


#5 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 23 June 2015 - 05:41 PM

so with this the result will come in a table?

I need to use the table or i can make with other thing?

#6 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 23 June 2015 - 05:57 PM

ok , it works , thanks

#7 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 23 June 2015 - 06:19 PM

View Postlukasloko, on 23 June 2015 - 05:57 PM, said:

ok , it works , thanks

You are welcome!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users