CoderzHeaven

December 12, 2011

Most Common and most important String functions in Corona.

Here are some of the string functions that are most commonly used in Corona SDK.
In this example you can find all the common string functions with a wroking example for each.
This example contains functions to

1. Check the first character in a string
2. Check the last character in a string.
3. Changing the first character in a string to uppercase.
4. Toggling the case.
5. Splitting a string.
6. Email validation.
7. Getting the character from it’s ASCII value.
8. Finding a pattern in a string.
9. Splitting a sentence into words.
10. Changing to lowercase, uppercase and reversing a string.

print ("String functions in Corona");-- Function to check whether the first character is the given character. --function startWith(String, search_char)return(string.sub(String,1,string.len(search_char)) == search_char);end-- Function to check whether the last character is the given character. --function endsWith(String, search_char)return(string.sub(String,-string.len(search_char)) == search_char);end--Function to change the first character of a string to upper case --function changeFirstCharToUpperCase(String)return String:gsub("^%l", string.upper)endfunction ToggleCase(String)return string.gsub (String, "%f[%a]%u+%f[%A]", string.lower)end-- Iterating through words in a string.function getWords(String)local i = 1;for word in String:gmatch("%w+") doprint("word "..i.." : "..word);i = i+ 1;endendlocal str = "CoderzHeaven";local search_first_char = "C";local search_end_char = "n"if(startWith(str,search_first_char)) thenprint("startWith : "..search_first_char);elseprint ("Doesnot start with "..search_first_char);endif(endsWith(str,search_end_char)) thenprint("endsWith : "..search_end_char);elseprint ("Doesnot end with "..search_end_char);endstr = "coderzHeaven";print ("changeFirstCharToUpperCase : "..changeFirstCharToUpperCase(str))str = "CoderzHeaven Heaven of all working codes";print("ToggleCase : "..ToggleCase(str));getWords(str);--- Check whether this is a correct format of email address....email="coderzheaven@gmail.com"if (email:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")) thenprint(email .. " is a valid email address")elseprint('Invalid email address')end-- Get the charater from it's ASCII Value.....print('ASCII Value of char : '..string.char (65))--Looks for the first match of pattern in the string s.--If it finds a match, then find returns the indices of s--where this occurrence starts and ends; otherwise, it returns nil.print(string.find (str, 'der',2,false ));--Returns a formatted version of its variable number of arguments following--the description given in its first argument (which must be a string)print( string.format('%q', 'a string with "quotes" and n new line'));local s = "hello world from CoderzHeaven"for w in string.gmatch(s, "%a+") doprint(w)end--Find String Lengthprint("length : "..string.len(s));-- Change to Lowercaseprint(string.lower (s));--Reverse a stringprint(string.reverse (s));--Change to Uppercase.....print(string.upper (s))
Website PinFacebook TwitterMyspaceFriendfeedTechnoratidel.icio.usDiggGoogleStumbleUponPremium Responsive

Leave a Reply Cancel reply