forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

finding | in string

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Fri Mar 30, 2007 4:43 am    Post subject: finding | in string Reply with quote

Hey All,

I want to make a function that searches for | (pipe symbol) and returns each string between the mathes. I'm not very good with string.find or string.gmatch. I read the documentation but I still don't understand. What I want is:
Code:

str = "hello there,|How are you all?|Nice day isn't it?"

result:

hello there,
How are you all?
Nice day isn't it?


hope you understand. I want to use | as a divider for lines.[/code]
_________________
Behold! The Underminer got hold of a PSP
Back to top
View user's profile Send private message
Altair



Joined: 20 May 2006
Posts: 76
Location: The Netherlands

PostPosted: Sat Mar 31, 2007 9:57 pm    Post subject: Reply with quote

I know this function that was made by LuMo.

Code:
function explode(text_ex, needle)
   local array={}
   _, count = string.gsub(text_ex, needle, needle)
   for index=1,count+1 do
      i, j = string.find(text_ex, needle)
      if i == nil or j==nil then
         i=0
         j=0
      end
           table.insert(array,string.sub(text_ex, 1, i-1))
      text_ex=string.sub(text_ex, j+1 )
        end
        return array
end


Where:
text_ex is the string
needle is the divider

So in your case you would do:
Code:
newarray = explode(str, "|")
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group