{"id":82,"date":"2007-05-15T13:02:26","date_gmt":"2007-05-15T21:02:26","guid":{"rendered":"http:\/\/dougmccune.com\/blog\/2007\/05\/15\/multi-line-strings-in-actionscript-3\/"},"modified":"2013-06-09T20:21:54","modified_gmt":"2013-06-10T04:21:54","slug":"multi-line-strings-in-actionscript-3","status":"publish","type":"post","link":"https:\/\/dougmccune.com\/blog\/2007\/05\/15\/multi-line-strings-in-actionscript-3\/","title":{"rendered":"Multi-line strings in Actionscript 3"},"content":{"rendered":"<p>Jesus, this took me a bazillion hours to figure out.<\/p>\n<p>OK, so I wanted to set a String variable in Actionscript. The actual value I wanted was a block of Javascript code (more on why in a second). In <a href=\"http:\/\/us2.php.net\/heredoc\">PHP<\/a> we&#8217;re allowed to use something called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Here_document\">heredoc<\/a> syntax. So I could write my code like this:<\/p>\n<p><code>$str = <<<EOD\nExample of string\nspanning multiple lines\nusing heredoc syntax.\nEOD;<\/code><\/p>\n<p>and then my variable would contain the multi-line string with line breaks and all. Sweet, that's what I wanted to do. Turns out AS3 doesn't support heredoc syntax. So that sucks. <\/p>\n<p>I spent the next few hours banging my head on a wall trying to figure out how to cut and paste my long block of code that I wanted to get into a String to work correctly in my AS code. Googling \"actionscript heredoc\" gave me nothing. Googling \"actionscript multi-line string\" gave me nothing. The golden nugget was finally realizing that AS3 is basically Javascript 2, so maybe some JS guys had figured this one out. Turns out they did. Here's the <a href=\"http:\/\/mook.wordpress.com\/2005\/10\/30\/multi-line-strings-in-javascript\/\">JS nugget<\/a> that led me to my solution. <\/p>\n<p>You can use the same method in Actionscript 3. So your AS code would look like:<\/p>\n<pre><code>private var myString:String = ( <![CDATA[\r\n\t\t\t\tHere is my string \r\n\t\t\t\tthat spans multiple \r\n\t\t\t\tlines.\r\n\t\t\t\t]]> ).toString();<\/code><\/pre>\n<p>It's easier in MXML, in that case all you need to do is something like: <\/p>\n<pre><code><mx:String id=\"myString\">\r\n\t<![CDATA[\r\n\t\tHere is my string \r\n\t\tthat spans multiple \r\n\t\tlines.\r\n\t]]>\r\n<\/mx:String><\/code><\/pre>\n<p>So that's how you simulate the heredoc syntax in AS3. <\/p>\n<p>And a quick note: when I was trying to figure this out I was making a class that injected Javascript code into the HTML wrapper for a component to execute. See <a href=\"http:\/\/www.abdulqabiz.com\/blog\/archives\/macromedia_flex\/a_mxml_componen.php\">Abdul Qabiz's post here<\/a> for a description of this method and a utility class to help you do this. Basically this let me write a component that used both Actionscript and Javascript and I could keep all my AS code <strong>and<\/strong> my JS code within the Actionscript component (as opposed to putting in the HTML wrapper). That's pretty frickin cool.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jesus, this took me a bazillion hours to figure out. OK, so I wanted to set a String variable in Actionscript. The actual value I wanted was a block of Javascript code (more on why in a second). In PHP we&#8217;re allowed to use something called heredoc syntax. So I could write my code like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[7],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-flex","tag-actionscript"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":1,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":1802,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions\/1802"}],"wp:attachment":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}