GAS – Strings in Apps Script


Strings are text values and you’ll use them a lot while programming. In fact, all the words in this post are stored as one very large string in this website’s database.

Every string value must start and begin with either a single or double quotation mark. You must use the same type of quotation mark (single or double) at the beginning and at the end of the string.

Examples of strings:

  • “This is a string enclosed within double quotation marks.”
  • ‘This string prefers single quotation marks.’
  • “Strings can contain any type of character. Example: $%$(*^&^^%%$#$1230___~.”

Prerequisites

Concatenating strings

To concatenate two strings (i.e., to join them together), use the concatenation operator (+). Both the concatenation operator and the addition operator have the same symbol + but Apps Script will figure out which operation to perform based on the values being operated on. If the values are numbers, Apps Script will add them but if they’re strings, they’ll be concatenated.

Examples:

  • "abc" + "def" // will result in “abcdef”
  • “1” + “2” // will result in “12” since 1 and 2 are strings and not numbers
  • “1” + 2 // will also result in “12” (Remember type conversion? Since “1” is a string and 2 is a number, Apps Script will convert the number into a string and then will concatenate the two strings together.)

Appending a string to another string

You can append a value to a string by using the += operator. For example, let us say you want to append the string “World” to the string “Hello”. The resulting output will be “HelloWorld”.

You can achieve this using string concatenation.

var str = "Hello";
str = str + "World";
Logger.log(str);

You can also use the += operator to achieve the same result.

var str = "Hello";
str += "World";
Logger.log(str);

The statement str += "World" is an abbreviated version of str = str + "World".

The Escape character

Sometimes you may want Apps Script to treat certain characters in a special way. For example, what if a string has quotation marks in it?

Consider the following string: 'Let's go to the park today'. The single quotation mark in the word “let’s” will make Apps Script think that the string has ended but this mark is actually a part of the string.

'Let's go to the park today'

To fix this, we need Apps Script to treat the single quotation mark in the word “let’s” just like any other character that is a part of the string. An escape character makes this possible.

If you prefix the single quotation mark in “let’s” with a backslash (\), it will not terminate the string and will instead be treated like a regular character that is a part of the string.

'Let\'s go to the park today'

The backslash is called an escape character and it makes Apps Script treat certain characters that follow it in a special way. Specifically:

  • \': Apps Script treats the single quotation mark just like any other character in the string.
  • \": Apps Script treats the double quotation mark just like any other character in the string.
  • \n: Apps Script inserts a new line when it encounters a “\n” in the string. The characters that follow a \n will be printed on the next line.
  • \\: Apps Script treats the backslash character just like any other character in the string. That’s right, you can even escape the escape character!

Conclusion

In this tutorial, you learned about Strings in Apps Script.

  • Strings are the text values used by your program.
  • Every string value must start and begin with either a single or double quotation mark.
  • To concatenate two strings (i.e., to join them together), use the concatenation operator (+).
  • You can append a value to a string by using the += operator.
  • Use escape characters to make Apps Script treat certain characters in a special way.
Nandemo Webtools

Leave a Reply