{"id":80365,"date":"2016-10-28T11:59:45","date_gmt":"2016-10-28T18:59:45","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80365"},"modified":"2019-02-18T09:10:23","modified_gmt":"2019-02-18T16:10:23","slug":"powertip-know-the-difference-between-the-split-method-and-split","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-know-the-difference-between-the-split-method-and-split\/","title":{"rendered":"PowerTip: Know the difference between the .split() method and \u2018-split\u2019"},"content":{"rendered":"<p><strong>Summary<\/strong>: Cloud and Datacenter Management MVP, Thomas Rayner, shows how to split a string by using a string instead of just a character.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> I am trying to split the string \u201cthis is my amazing string\u201d on the pattern \u201cmy\u201d by using \u201cthis is my amazing string\u201d.split(\u201cmy\u201d) but it\u2019s giving me a bunch of garbled stuff back. How do I accomplish my goal?<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0You\u2019ll have to use <strong>\u2013split<\/strong> instead of <strong>.split()<\/strong>. Methods like <strong>.split()<\/strong> and <strong>.trim()<\/strong> take arrays of characters instead of strings. What happened to you is that your pattern, \u201cmy\u201d, was treated as an array of characters and \u201cthis is my amazing string\u201d was split on all of the \u201cm\u201ds and \u201cy\u201ds in the string. Use the <strong>\u2013split<\/strong> method instead.<\/p>\n<p style=\"padding-left: 90px\"><code>\u201cthis is my amazing string\u201d -split \u201cmy\u201d<\/code><\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\"The Doctor\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Cloud and Datacenter Management MVP, Thomas Rayner, shows how to split a string by using a string instead of just a character. I am trying to split the string \u201cthis is my amazing string\u201d on the pattern \u201cmy\u201d by using \u201cthis is my amazing string\u201d.split(\u201cmy\u201d) but it\u2019s giving me a bunch of garbled stuff [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,639,687,641],"tags":[56,356,652,45],"class_list":["post-80365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-powertip","category-regular-expressions","category-windows-powershell","tag-guest-blogger","tag-powertip","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Cloud and Datacenter Management MVP, Thomas Rayner, shows how to split a string by using a string instead of just a character. I am trying to split the string \u201cthis is my amazing string\u201d on the pattern \u201cmy\u201d by using \u201cthis is my amazing string\u201d.split(\u201cmy\u201d) but it\u2019s giving me a bunch of garbled stuff [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=80365"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80365\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=80365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}