Tutorial: Lua string magic

string-magic-feat
Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

This tutorial features Lua string manipulation and parsing techniques which can be useful in several applications, especially business or database-powered apps where you need to check and confirm that text is formatted properly. These methods “enhance” the native Lua string library and come to you courtesy of the Lua Users Wiki where you can find various Lua tidbits and code samples.

Splitting strings

Do you have some data that’s in the form of a long string and you need to break it up into smaller strings based on a custom pattern? A common example is street addresses:

Now, let’s assume you want to parse out each piece of data separated by the comma. Lua does not have a “split” function native to its string library, but with a small function, you can include the same functionality. Inspect the following code:

This function accomplishes the desired task gracefully, provides a user-defined pattern to split on, and allows for an optional table to append the strings to.

Both methods of calling the function are valid:

Trimming strings

Trimming strings can be useful in several cases, especially if you’re working with data from an external source, or data that is input by the user via the device’s soft keyboard. As with the “split” functionality, string trimming is not native to Lua’s string library, but it can be accomplished easily enough. Inspect the following function:

Format as “title case”

Occasionally it’s useful to convert a string into “title case,” in which the first letter of each word is converted to uppercase while the remaining letters are set as lowercase. As an example, let’s assume that a name was entered in a very sloppy format like:

Obi-waN kEnObi

Of course, you probably want to correct this, and doing so requires just a few lines of code:

The result is exactly as expected: Obi-Wan Kenobi.

Strict line wrapping

Sometimes you’ll have a long string (paragraph) of text which needs to be line-wrapped at a specific character interval, not a pixel distance. Corona’s display.newText() API handles line wrapping if you provide the width parameter, but if you want interval-specific control, you might prefer the following function which allows you to set a maximum number of characters per line as the limit argument. In addition, it allows you to set indent values — as optional strings, not integers — for both the start of the paragraph and/or each following line.

Let’s examine the arguments in order:

  • str (required string) — The string of text which you want to line-wrap.
  • limit (optional integer) — The maximum interval at which to wrap the lines.
  • indent (optional string) — Indention string for each line, i.e. [2 spaces].
  • indent1 (optional string) — Indention string for the first line only, i.e. [6 spaces].

Another benefit of this function versus auto-wrapping is that you can “rewrap” the text to a new interval without changing or recreating the text display object itself. The following example rewraps the same text to an interval limit of 16 instead of the initial limit of 36. Of course, you can also change the text entirely and/or adjust the indent values.

Confirm email format

Another useful string check is confirming that a user has entered an email address in the proper format. Of course, this function can’t check if the actual address is valid and functional, but at least it confirms the format.

In summary

That wraps up today’s tutorial on Lua string “magic.” Hopefully you can find a place for these in your apps!


Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0
Rob Miracle

Rob Miracle creates mobile apps for his own enjoyment and the amusement of others. He serves the Corona Community in the forums, on the blog, and at local events.

This entry has 6 replies

  1. Mustafa A. says:

    Nice tutorial Rob and very useful techniques! Please add more in future posts.

    I have a Lua question, let’s assume we have this string:

    local initialText = “Lorem ipsum dolor sit amet consetetur”

    How can I trim the string based on number of character letters?

    So if limit is 11, then it will be like this

    local finalText = “Lorem ipsum”

    Thank you,
    Mustafa A.

    • Rob Miracle says:

      The string.sub() API call built into the library will do that:

      local finalText = initialText:sub(1,11)

      • Mustafa A. says:

        Thank you Rob,

        It worked but I get a weird question mark at the end of the string after it’s trimmed.

        Is it because I’m using Arabia characters? Is it some kind of unicode porblem?

        noon[i] = tableOnDemand.posts[i].title
        noon[i] = noon[i]:sub(1,50) . . “…”
        featuredTileTitle[i] = display.newText(noon[i], 0, 0, 180, 100, native.systemFontBold, 16)

        Any idea?

  2. Suhada says:

    Thanks for another great tutorial.
    I’d love to see a tutorial taking this theme of “business or database-powered apps” further – describing how to allow user input of a range of variables:
    Inputting some strings, some numbers, some options, preferably enough to mean scrolling is necessary and preferably something which will also run on the simulator too.

  3. Hi,
    Nice tutorial. Shameless plug, those who are interested in can take a look at Allen, a work of mine (See here: https://github.com/Yonaba/Allen). It is a simple utility library for strings, offering a wide range of similar functions and more.

    Regards,
    Roland.

  4. Steve Taylor says:

    Roland, very nice looking library, I was able to look at the code and get a pretty good idea of what it did. I selected the documentation link and got a 404 error, should I plan on just using the code for docs or was there just an error in setting up the docs? Thanks for the nice library!

Leave a reply

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">