finding | in string

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

finding | in string

Post by the underminer »

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: Select all

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
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

I know this function that was made by LuMo.

Code: Select all

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: Select all

newarray = explode(str, "|")
Post Reply