{"id":4207,"date":"2023-05-14T10:06:05","date_gmt":"2023-05-14T10:06:05","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4207"},"modified":"2023-06-10T11:29:20","modified_gmt":"2023-06-10T11:29:20","slug":"gas-strings-in-apps-script","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/gas-strings-in-apps-script\/","title":{"rendered":"GAS &#8211; Strings in Apps Script"},"content":{"rendered":"<p>Strings are text values and you&#8217;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&#8217;s database.<\/p>\n<p class=\"para\">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.<\/p>\n<p class=\"para\">Examples of strings:<\/p>\n<ul class=\"list\">\n<li class=\"list-item\">&#8220;This is a string enclosed within double quotation marks.&#8221;<\/li>\n<li class=\"list-item\">&#8216;This string prefers single quotation marks.&#8217;<\/li>\n<li class=\"list-item\">&#8220;Strings can contain any type of character. Example: $%$(*^&amp;^^%%$#$1230___~.&#8221;<\/li>\n<\/ul>\n<h2 class=\"content-header\">Prerequisites<\/h2>\n<div class=\"note\" role=\"alert\">\n<p class=\"para\">This tutorial assumes that you have completed the previous tutorials in this series OR are familiar with the following concepts:<\/p>\n<\/div>\n<ul class=\"list\">\n<li class=\"list-item\"><a class=\"anchor\" href=\"https:\/\/rengga.dev\/blog\/gas-why-should-you-learn-apps-script\/\" target=\"_blank\" rel=\"noopener\"><u>What is Apps Script?<\/u><\/a><\/li>\n<li class=\"list-item\"><a class=\"anchor\" href=\"https:\/\/rengga.dev\/blog\/gas-creating-your-first-apps-script\/\" target=\"_blank\" rel=\"noopener\"><u>How to create and run scripts?<\/u><\/a><\/li>\n<li class=\"list-item\"><a class=\"anchor\" href=\"https:\/\/rengga.dev\/blog\/gas-values-types-and-operators-in-apps-script\/\" target=\"_blank\" rel=\"noopener\"><u>Values, types and operators in Apps Script<\/u><\/a><\/li>\n<li class=\"list-item\"><u>Variables and constants in Apps Script<\/u><\/li>\n<\/ul>\n<h2 id=\"concatenating-strings-apps-script\" class=\"content-header\">Concatenating strings<\/h2>\n<p class=\"para\">To concatenate two strings (i.e., to join them together), use the concatenation operator (<code class=\"inline-code\">+<\/code>). Both the concatenation operator and the addition operator have the same symbol\u00a0<code class=\"inline-code\">+<\/code>\u00a0but 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&#8217;re strings, they&#8217;ll be concatenated.<\/p>\n<h3 class=\"content-sub-header\">Examples:<\/h3>\n<ul class=\"list\">\n<li class=\"list-item\"><code class=\"inline-code\">\"abc\" + \"def\"<\/code>\u00a0\/\/ will result in &#8220;abcdef&#8221;<\/li>\n<li class=\"list-item\">&#8220;1&#8221; + &#8220;2&#8221; \/\/ will result in &#8220;12&#8221; since 1 and 2 are strings and not numbers<\/li>\n<li class=\"list-item\">&#8220;1&#8221; + 2 \/\/ will also result in &#8220;12&#8221; (Remember type conversion? Since &#8220;1&#8221; 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.)<\/li>\n<\/ul>\n<h2 id=\"append-operator-strings-google-apps-script\" class=\"content-header\">Appending a string to another string<\/h2>\n<p class=\"para\">You can append a value to a string by using the\u00a0<code class=\"inline-code\">+=<\/code>\u00a0operator. For example, let us say you want to append the string &#8220;World&#8221; to the string &#8220;Hello&#8221;. The resulting output will be &#8220;HelloWorld&#8221;.<\/p>\n<p class=\"para\">You can achieve this using string concatenation.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var str = \"Hello\";\r\nstr = str + \"World\";\r\nLogger.log(str);<\/pre>\n<p>You can also use the += operator to achieve the same result.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var str = \"Hello\";\r\nstr += \"World\";\r\nLogger.log(str);<\/pre>\n<p class=\"para\">The statement\u00a0<code class=\"inline-code\">str += \"World\"<\/code>\u00a0is an abbreviated version of\u00a0<code class=\"inline-code\">str = str + \"World\"<\/code>.<\/p>\n<h2 id=\"escape-character-string-apps-script\" class=\"content-header\">The Escape character<\/h2>\n<p class=\"para\">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?<\/p>\n<p class=\"para\">Consider the following string:\u00a0<code class=\"inline-code\">'Let's go to the park today'<\/code>. The single quotation mark in the word &#8220;let&#8217;s&#8221; will make Apps Script think that the string has ended but this mark is actually a part of the string.<\/p>\n<p class=\"para\"><code class=\"inline-code\">'Let's go to the park today'<\/code><\/p>\n<p class=\"para\">To fix this, we need Apps Script to treat the single quotation mark in the word &#8220;let&#8217;s&#8221; just like any other character that is a part of the string. An escape character makes this possible.<\/p>\n<p class=\"para\">If you prefix the single quotation mark in &#8220;let&#8217;s&#8221; 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.<\/p>\n<p class=\"para\"><code class=\"inline-code\">'Let\\'s go to the park today'<\/code><\/p>\n<p class=\"para\">The backslash is called an escape character and it makes Apps Script treat certain characters that follow it in a special way. Specifically:<\/p>\n<ul class=\"list\">\n<li class=\"list-item\"><code class=\"inline-code\">\\'<\/code>: Apps Script treats the single quotation mark just like any other character in the string.<\/li>\n<li class=\"list-item\"><code class=\"inline-code\">\\\"<\/code>: Apps Script treats the double quotation mark just like any other character in the string.<\/li>\n<li class=\"list-item\"><code class=\"inline-code\">\\n<\/code>: Apps Script inserts a new line when it encounters a &#8220;<code class=\"inline-code\">\\n<\/code>&#8221; in the string. The characters that follow a\u00a0<code class=\"inline-code\">\\n<\/code>\u00a0will be printed on the next line.<\/li>\n<li class=\"list-item\"><code class=\"inline-code\">\\\\<\/code>: Apps Script treats the backslash character just like any other character in the string. That&#8217;s right, you can even escape the escape character!<\/li>\n<\/ul>\n<h2 class=\"content-header\">Conclusion<\/h2>\n<p>In this tutorial, you learned about Strings in Apps Script.<\/p>\n<ul class=\"list\">\n<li class=\"list-item\">Strings are the text values used by your program.<\/li>\n<li class=\"list-item\">Every string value must start and begin with either a single or double quotation mark.<\/li>\n<li class=\"list-item\">To\u00a0<u>concatenate<\/u>\u00a0two strings (i.e., to join them together), use the concatenation operator (<code class=\"inline-code\">+<\/code>).<\/li>\n<li class=\"list-item\">You can\u00a0<u>append a value to a string<\/u>\u00a0by using the\u00a0<code class=\"inline-code\">+=<\/code>\u00a0operator.<\/li>\n<li class=\"list-item\">Use\u00a0<u>escape characters<\/u>\u00a0to make Apps Script treat certain characters in a special way.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Strings are text values and you&#8217;ll use them a lot while programming. <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/gas-strings-in-apps-script\/\" title=\"GAS &#8211; Strings in Apps Script\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":4379,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[398,12],"tags":[503,399,400,402,401,403,528,507,506],"newstopic":[588],"class_list":{"0":"post-4207","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-google-apps-script","8":"category-web-development","9":"tag-custom-functions-for-google-sheets","10":"tag-gas","11":"tag-google-apps-script","12":"tag-google-apps-script-free-code","13":"tag-google-apps-script-sample","14":"tag-google-apps-script-source","15":"tag-strings","16":"tag-types-and-operators","17":"tag-values","18":"newstopic-google-app-script"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=4207"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4207\/revisions"}],"predecessor-version":[{"id":4208,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4207\/revisions\/4208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/4379"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=4207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4207"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}