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.
Tutorials
Formatting values using “string.format()”
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.
Lua string magic
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.
Utilizing time and dates
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.
Further reading
- Lua String Manipulation guide
- string.format() documentation
- string.match() documentation
Conclusion
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.
Oleg
Posted at 22:01h, 29 OctoberIs it any way for rich formatting (bold, color, tabulation) on Corona?
Rob Miracle
Posted at 18:21h, 01 NovemberYou can use a native.newWebView() to show HTML/CSS/Javascript styled code. A few community developers have made libraries that will let you style text, but we have no formal support for it.