27 October 2015
Tutorial Treasury: String formatting and manipulation
As any Corona developer knows, Lua variables come in many forms — most commonly tables (arrays), numbers, strings, and booleans (true/false). Among these, strings present a nearly limitless range of possibilities, as they can be of considerable length, vary in content, and contain special characters that no other variable type can. This can present some challenge to Corona developers, especially when it comes time to “select or detect” a specific portion within a longer string or format the string in a very custom, specific manner.
This tutorial treasury contains some useful and often essential resources on how to work with strings in your Corona apps.
Formatting a particular string is a common requirement in almost any app, whether it be limiting the number of decimal places in a long number, adding “placeholder” spaces, or padding a score with leading zeros. The string.format() function is almost infinitely powerful for this task, but many developers are mystified by its parameters and usage. This tutorial aims to dispel some of the confusion surrounding string formatting.
Sometimes, formatting strings becomes an even more specific process, and a little “magic” is required to achieve the end goal. In this tutorial, learn how to overcome special formatting challenges including splitting, trimming, line wrapping, and more.
Many apps rely on time and dates, and it can be tricky to properly format time/date-based data, particularly strings gathered from os.time() and os.date(). In this tutorial, learn how to use string functions to format time and dates exactly as required in your apps.
As illustrated, string formatting and manipulation is a common but often misunderstood process. Hopefully this collection of resources gets you started on the path toward understanding and mastery of string-related tasks.