String Manipulation

Hello.

I’m struggling to filter this string:

s = '\<?xml version="1.0" encoding="utf-8"?\>'

I need to check if encoding is utf-8 or not.

I tried string.match, string.find, string.gsub, all returned nil.

can someone help me build the right line? or even better a good place to learn string manipulation. corona docs sucks in this department.

thanks,

Carlos.

The function listed here will try to both fix a UTF-8 string and will tell you if it is invalid.  

http://notebook.kulchenko.com/programming/fixing-malformed-utf8-in-lua

Hope it helps.  If not I’d just do some searches on LUA and UTF8 till you find a pure LUA solution.

PS - Corona docs, by the way are pretty awesome, but they aren’t here to teach Lua. :\

is your question just:  does this string contain the literal “utf-8”?

if so, it’s probably the dash tripping you up.  it’s a special character, need to escape it in your pattern

if (s:find("utf%-8")) then

(should also force upper or lower case throughout)

roaminggamer, i’m not trying to fix a UTF-8. that i already know. thanks :slight_smile:

i was really trying to check if the file have the word 'encoding=“utf-8”. if so i do nothing, because it’s already encoded if the file had another encode type i convert to utf8.

davebollinger answer was the one i was after. thank you very much Dave.

yes, it was the dash was triping. i thought it was the " " " …i forgot " - " was a special char to :slight_smile:

the check i wanted is

if (s:find('encoding="utf%-8"')) then

thank you both for the replies.

The function listed here will try to both fix a UTF-8 string and will tell you if it is invalid.  

http://notebook.kulchenko.com/programming/fixing-malformed-utf8-in-lua

Hope it helps.  If not I’d just do some searches on LUA and UTF8 till you find a pure LUA solution.

PS - Corona docs, by the way are pretty awesome, but they aren’t here to teach Lua. :\

is your question just:  does this string contain the literal “utf-8”?

if so, it’s probably the dash tripping you up.  it’s a special character, need to escape it in your pattern

if (s:find("utf%-8")) then

(should also force upper or lower case throughout)

roaminggamer, i’m not trying to fix a UTF-8. that i already know. thanks :slight_smile:

i was really trying to check if the file have the word 'encoding=“utf-8”. if so i do nothing, because it’s already encoded if the file had another encode type i convert to utf8.

davebollinger answer was the one i was after. thank you very much Dave.

yes, it was the dash was triping. i thought it was the " " " …i forgot " - " was a special char to :slight_smile:

the check i wanted is

if (s:find('encoding="utf%-8"')) then

thank you both for the replies.