{"id":999,"date":"2014-07-16T00:01:00","date_gmt":"2014-07-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/16\/join-me-in-a-few-string-methods-using-powershell\/"},"modified":"2014-07-16T00:01:00","modified_gmt":"2014-07-16T00:01:00","slug":"join-me-in-a-few-string-methods-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/join-me-in-a-few-string-methods-using-powershell\/","title":{"rendered":"Join Me in a Few String Methods Using PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using the <\/span><b style=\"font-size:12px\">String<\/b><span style=\"font-size:12px\"> class of the <\/span><b style=\"font-size:12px\">Join<\/b><span style=\"font-size:12px\"> method in Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning while I was eating breakfast and reviewing my email on my Surface&nbsp;2 Pro, I noticed that I have meetings all afternoon. Luckily, I was up early, and so I can get my writing done before the meetings start. The good thing is that I love meetings. Or, perhaps, I should say, that I love good meetings. Useless meetings are, well, useless.<\/p>\n<p>But a good meeting is great because it lets me collaborate with brilliant people, and I always leave the meeting in a better mood than when I showed up. In fact, good meetings become the highlight of my day. Today&rsquo;s meetings have the potential to be awesome, so I am looking forward to spending the afternoon collaborating with some of the brilliant people who are on my team. It will be awesome.<\/p>\n<h2>Using the String class static join method<\/h2>\n<p>The .NET Framework contains a class in the System namespace named <b>String<\/b>. The <b>String<\/b> class contains a number of methods and a few properties and other things. Complete documentation for the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/System.String(v=vs.110).aspx\" target=\"_blank\">String Class<\/a> resides on MSDN. A number of the methods of the <b>String<\/b> class are <i>static <\/i>methods. This means that they are always available, and that I do not have to create a string (an instance of the class) to gain access to the method. The <b>Join<\/b><i> <\/i>method is one of these static methods.<\/p>\n<p style=\"margin-left:30px\"><b>Dr. Scripto says:<\/b><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8764.Bubble.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8764.Bubble.png\" alt=\" Image of text\" width=\"468\" height=\"153\" title=\" Image of text\" \/><\/a><\/p>\n<p style=\"margin-left:240px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1538.dr_scripto_anim.gif\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1538.dr_scripto_anim.gif\" alt=\" Image of Dr. Scripto\" width=\"145\" height=\"208\" title=\"Image of Dr. Scripto\" \/><\/a><\/p>\n<p>MSDN lists five overloads for the static <b>Join<\/b><i> <\/i>method of the <b>System.String<\/b> class. Today, I want to talk about three of these overloads. The following table explains the three ways of calling the <b>Join<\/b> method.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"217\" valign=\"top\">\n<p><b>Method overload<\/b><\/p>\n<\/td>\n<td width=\"420\" valign=\"top\">\n<p><b>Description<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"217\" valign=\"top\">\n<p>&nbsp; Join(String, Object[])<\/p>\n<\/td>\n<td width=\"420\" valign=\"top\">\n<p>Concatenates the elements of an object array by using the specified separator between each element.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"217\" valign=\"top\">\n<p>&nbsp; Join(String, String[])<\/p>\n<\/td>\n<td width=\"420\" valign=\"top\">\n<p>Concatenates all the elements of a string array by using the specified separator between each element.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"217\" valign=\"top\">\n<p>&nbsp; Join(String, String[], Int32, Int32)<\/p>\n<\/td>\n<td width=\"420\" valign=\"top\">\n<p>Concatenates the specified elements of a string array by using the specified separator between each element.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"font-size:1.5em\">Joining objects<\/span><\/h2>\n<p>One of the cool things that the <b>Join<\/b> method can do is join elements of an array of objects. It also permits me to specify what I want to use as a separator between each of the elements that is joined. But beyond being cool, this element is very convenient because it lets me join the elements together without having to explicitly convert the object elements to strings. A string representation of each object in the array is automatically derived by calling the <b>tostring()<\/b><i> <\/i>method from the object. This method works well, and it uses this type of syntax:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"213\" valign=\"top\">\n<p><b>Method<\/b><\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p><b>Parameter one<\/b><\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p><b>Parameter two<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"213\" valign=\"top\">\n<p>[String]::Join(<\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p>Separator<\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p>Array of objects)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In this example, I use the <b>Get-Process<\/b> cmdlet to obtain a collection of system.diagnostics.process objects. I then display the objects and separate them by using the pipe character. The commands are:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $gps = Get-Process<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::Join(&quot;|&quot;,$gps)<\/p>\n<p>The commands and the output associated with the commands are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-16-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-16-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>So that is not too impressive. Beginning with Windows PowerShell&nbsp;3.0, I can use the <i>automatic foreach <\/i>technique to get a single property from each object in the collection. When I use this technique, the results are much more impressive. The following command will retrieve each name of each process and join the output:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $gps = Get-Process<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::Join(&quot;|&quot;,$gps.name)<\/p>\n<p>The command and the output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-16-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-16-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Because Windows PowerShell is object oriented, and because everything is an object in Windows PowerShell, this particular overload of the <b>Join<\/b><i> <\/i>method is one of my favorites.<\/p>\n<h2>Joining an array of strings<\/h2>\n<p>The second way to use the <b>Join<\/b><i> <\/i>static method that I want to look at today is the one I call &ldquo;Hey, let&#039;s glue together a group of strings.&rdquo; Or more briefly, it is the join an array of strings method. This method is really easy to use, and it acts exactly the way I would expect it to. The following tables shows the pattern for this.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"213\" valign=\"top\">\n<p><b>Method<\/b><\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p><b>Parameter one<\/b><\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p><b>Parameter two<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"213\" valign=\"top\">\n<p>[String]::Join(<\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p>Separator<\/p>\n<\/td>\n<td width=\"213\" valign=\"top\">\n<p>Array of strings)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In this example, the first thing I need to do is to create an array of strings. After I create the array of strings, I will call the static <b>Join<\/b><i> <\/i>method and join together the array while using a semicolon for the separator between each of the elements.<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $arrayOfStrings = &quot;this&quot;, &quot;is&quot;, &quot;an&quot;, &quot;array&quot;, &quot;of&quot;, &quot;strings&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $separator = &quot;;&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::Join($separator,$arrayOfStrings)<\/p>\n<p style=\"margin-left:30px\">this;is;an;array;of;strings<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h2>Joining specific elements of an array of strings<\/h2>\n<p>One of the way cool things to do using the <b>Join<\/b><i> <\/i>static method is to select a subset of the elements from an array of strings. The method call looks like the following:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"148\" valign=\"top\">\n<p><b>Method<\/b><\/p>\n<\/td>\n<td width=\"100\" valign=\"top\">\n<p><b>Param 1<\/b><\/p>\n<\/td>\n<td width=\"126\" valign=\"top\">\n<p><b>Param 2<\/b><\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p><b>Param 3<\/b><\/p>\n<\/td>\n<td width=\"105\" valign=\"top\">\n<p><b>Param 4<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"148\" valign=\"top\">\n<p>[String]::Join(<\/p>\n<\/td>\n<td width=\"100\" valign=\"top\">\n<p>Separator<\/p>\n<\/td>\n<td width=\"126\" valign=\"top\">\n<p>Array of strings<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Start index<\/p>\n<\/td>\n<td width=\"105\" valign=\"top\">\n<p>Count)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the following example, I first create an array of strings and store it in a variable I call <b>$arrayOfStrings<\/b>. I next create a variable named <b>$separator<\/b> and I assign a blank space to it. Next, I create a variable named <b>$start<\/b> and a variable named <b>$count<\/b>, and I assign the value of 3 to them.<\/p>\n<p>Now I call the static <b>Join<\/b> method and I specify the separator to use, the array of strings, the start index, and the count. The result is that I join the last three words from my array. This command and output are shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $arrayOfStrings = &quot;this&quot;, &quot;is&quot;, &quot;an&quot;, &quot;array&quot;, &quot;of&quot;, &quot;strings&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $separator = &quot; &quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $start = 3<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $count = 3<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; [string]::Join($separator,$arrayOfStrings, $start, $count)<\/p>\n<p style=\"margin-left:30px\">array of strings<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>That is all there is to using the <b>String<\/b> class to join strings together. String Week will continue tomorrow when I will talk about splitting strings.<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the String class of the Join method in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. This morning while I was eating breakfast and reviewing my email on my Surface&nbsp;2 Pro, I noticed that I have meetings all afternoon. Luckily, I was up early, and [&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":[1],"tags":[3,4,336,45],"class_list":["post-999","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-strings","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the String class of the Join method in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. This morning while I was eating breakfast and reviewing my email on my Surface&nbsp;2 Pro, I noticed that I have meetings all afternoon. Luckily, I was up early, and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/999","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=999"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/999\/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=999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}