Watchmaker Wiki

The ultimate watch maker for Android Wear!

User Tools

Site Tools


Sidebar

tips:quotesinstrings

How to deal with apostrophes and quotes in strings

Examples

 string.sub('{c1t}',1,15)
 string.sub("{c1t}",1,15)
 string.sub([[{c1t}]],1,15)

Apostrophes, Quotes, and Braces (Oh, my!)

Dealing with strings is usually pretty straightforward. However, in LUA formulas, you're often mixing these strings with mathematical or string manipulation functions that require the use of some sort of string identifier. Most people use apostrophes (single-quotes) or quotation marks (double-quotes) for this. LUA doesn't usually care which identifier you use, but if your string happens to use the same character as your identifier, LUA can get confused.

Okay, what does that mean? Basically, it means that if your string has apostrophes, then you can't use apostrophes as identifiers. Take the following:

 string.len('This way to John's house')

Everything is great until you hit that apostrophe in the word “John's”. Suddenly, the string function thinks that it's reached the end of the string, and all hell breaks loose. Okay, not really, but the formula will certainly fail.

So how do we prevent this? Easy. Just don't use the same identifier. If your string has apostrophes, use quotes as the identifier. If it has quotes, use apostophes as the identifier. But what if it's a variable, and you don't know what it will have, such as with a calendar text item? No problem. In that case, just use double brackets, like this:

 string.sub([[{c1t}]],1,15)

This way, your text item can have quotes OR apostrophies, and it will still be fine. It can't have double-brackets, but if you're in the habit of doing that in your calendar items… well… stop it!

tips/quotesinstrings.txt · Last modified: 2015/02/06 17:19 by jt3