lua-users home
lua-l archive

Re: String manipulation

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2014年10月31日 19:33 GMT+02:00 Emeka <emekamicro@gmail.com>:
> mystring = "google+facebook"
...
> if mystring.sub(i,i) == ' " ' then .... end It is not comparing at all ,and
> this also failed.
Well, mystring does not contain a double-quote at all: those
double-quotes are the string delimiters.
So for a start you need something like:
mystring = [["google+facebook"]]
In general, the moment I need to put any string searches
inside a for loop I know I have missed a trick.
For example: find whether mystring contains a substring
between two double-quotes.
Method 1
local p,q
for i = 1,#mystring do if mystring:sub(i,i)=='"' then
 if not p then p=i
 elseif not q then q=i; break
 end
end end
if q then -- substring found
end
Method 2:
p, q = mystring:find[[%b""]]
if q then -- substring found
end

AltStyle によって変換されたページ (->オリジナル) /