string.sub

Hi,
I was trying to work with strings and manipulating them using the string.sub, when I found that there is an issue with the sub command, now I am not sure if that is a Lua thing or a Corona thing.

Generally the syntax of sub is sub(theString, startPos, lengthOfCharacters)

I was stumped when I did not get the required results using

local i  
  
for i=1,10 do --Assuming that the length of the string \*is\* 10 characters  
 local str = string.sub(theString, i ,1)  
 print ( str )  
end  

Now this was not working for me…any guesses at first look??

The issue is that the length is misleading, it requires the absolute positions, so sub is declared as

 sub(theString, startPos, startPos+length)

so the same works if you use

local str = string.sub(theString,i,i)  

I know it is silly but that’s how it is

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 4675 reply_id: 304675[/import]

this is set out in the Lua docs
http://lua-users.org/wiki/StringLibraryTutorial


s:sub(i [,j])

Return a substring of the string passed. The substring starts at i. If the third argument j is not given, the substring will end at the end of the string. If the third argument is given, the substring ends at and includes j.

> = string.sub(“Hello Lua user”, 7) – from character 7 until the end
Lua user
> = string.sub(“Hello Lua user”, 7, 9) – from character 7 until and including 9
Lua
> = string.sub(“Hello Lua user”, -8) – 8 from the end until the end
Lua user
> = string.sub(“Hello Lua user”, -8, 9) – 8 from the end until 9 from the start
Lua
> = string.sub(“Hello Lua user”, -8, -6) – 8 from the end until 6 from the end
Lua
[import]uid: 6645 topic_id: 4675 reply_id: 14810[/import]

hi jmp909, thanks for that link. Generally if someone refers to the links on the API page, they are insufficient and do not have enough examples. The best documented API reference I have ever read and owned was the Windows 3.1 API reference, a volume of 5 books, with references and code examples for each and every command. Why do we not have such detailed explanations anymore?

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 4675 reply_id: 14813[/import]

Lua is a hosted language within Corona with certain API functions exposed. If you want to get the best out of it you need to learn and refer to the official Lua documentation. (but bear in mind not all functions are available) [import]uid: 6645 topic_id: 4675 reply_id: 14904[/import]