{"id":182,"date":"2012-08-30T10:38:29","date_gmt":"2012-08-30T16:38:29","guid":{"rendered":"http:\/\/ryan.witschger.net\/?p=182"},"modified":"2012-08-30T10:38:29","modified_gmt":"2012-08-30T16:38:29","slug":"quick-tip-newlines-when-typecasting-arrays-to-strings","status":"publish","type":"post","link":"http:\/\/www.Get-Blog.com\/?p=182","title":{"rendered":"Quick Tip: Newlines when Typecasting Arrays to Strings"},"content":{"rendered":"<p>Converting arrays into strings can cause a couple different problems, and I am starting to see more and more people struggling. The problem is that it doesn&#8217;t always act like you think it should. By default, converting an array into a string will place every item on one line of text separated by a space. This is particularly true with arrays made by get-content that are typecast into a string.<\/p>\n<p>The answer is very simple. When you type cast an array into a string every field is separated by a secret value (not so secret anymore!) called the Object Field Separator, or OFS. This value is keep in a variable called <span class=\"lang:ps decode:true  crayon-inline \">$OFS<\/span> and you can change it! For instance, if you want there to be a new line when you typecast the array then set <span class=\"lang:ps decode:true  crayon-inline \">$OFS = &#8220;`r`n&#8221;<\/span> which will make a newline between every field. If you want to comma separate the values, then set <span class=\"lang:ps decode:true crayon-inline\">$OFS = &#8220;, &#8220;<\/span><\/p>\n<p>Just remember to clean up when you are done! You can set the value back to it&#8217;s default (a single space) by removing the variable: <span class=\"lang:ps decode:true  crayon-inline\">Remove-Variable OFS<\/span> Don&#8217;t worry, PowerShell will remake it.<\/p>\n<pre class=\"theme:psconsole lang:ps decode:true\">C:&gt; $Test = \"Ryan\", \"John\", \"Mark\"\nC:&gt; [String]$Test\nRyan Mark John\n\nC:&gt; $OFS = \"`r`n\"\nC:&gt; [String]$Test\nRyan\nMark\nJohn\n\nC:&gt; $OFS = \", \"\nC:&gt; [String]$Test\nRyan, Mark, John\n\nC:&gt; Remove-Variable OFS\nC:&gt; [String]$Test\nRyan Mark John<\/pre>\n<p>NOTE: The &#8216; that you are seeing should be the Backtick key found to the left of your numbers on a US keyboard.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converting arrays into strings can cause a couple different problems, and I am starting to see more and more people struggling. The problem is that it doesn&#8217;t always act like you think it should. By default, converting an array into a string will place every item on one line of text separated by a space. <a href='http:\/\/www.Get-Blog.com\/?p=182' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/182"}],"collection":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182"}],"version-history":[{"count":0,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}