Most Common and most important String functions in Corona.

By | December 12, 2011

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);

— 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);

–Function to change the first character of a string to upper case —
function changeFirstCharToUpperCase(String)
return String:gsub("^%l", string.upper)

function ToggleCase(String)
return string.gsub (String, "%f[%a]%u+%f[%A]", string.lower)

— Iterating through words in a string.
function getWords(String)
local i = 1;
for word in String:gmatch("%w+") do
print("word "..i.." : "..word);
i = i+ 1;

local str = "CoderzHeaven";
local search_first_char = "C";
local search_end_char = "n"

if(startWith(str,search_first_char)) then
print("startWith : "..search_first_char);
print ("Doesnot start with "..search_first_char);

if(endsWith(str,search_end_char)) then
print("endsWith : "..search_end_char);
print ("Doesnot end with "..search_end_char);

str = "coderzHeaven";
print ("changeFirstCharToUpperCase : "..changeFirstCharToUpperCase(str))

str = "CoderzHeaven Heaven of all working codes";
print("ToggleCase : "..ToggleCase(str));


— Check whether this is a correct format of email address….
if (email:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")) then
print(email .. " is a valid email address")
print(‘Invalid email address’)

— 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+") do

–Find String Length
print("length : "..string.len(s));

— Change to Lowercase
print(string.lower (s));

–Reverse a string
print(string.reverse (s));

–Change to Uppercase…..
print(string.upper (s))


Leave a Reply

Your email address will not be published. Required fields are marked *