{"id":1010,"date":"2014-07-14T00:01:00","date_gmt":"2014-07-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/14\/powershell-string-theory\/"},"modified":"2014-07-14T00:01:00","modified_gmt":"2014-07-14T00:01:00","slug":"powershell-string-theory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-string-theory\/","title":{"rendered":"PowerShell String Theory"},"content":{"rendered":"<p align=\"left\"><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with strings.<\/span><\/p>\n<p align=\"left\">Microsoft Scripting Guy, Ed Wilson, is here. This has been an exciting week around the Scripting Household. From our lanai, we can see several little rabbits hopping around&mdash;they seem to like the clover. We have seen some butterflies (which in past years, we have not seen), and the hummingbirds are back. It is a great time to be out and about. Sitting in a swing, observing nature is a fundamental that helps me keep a positive mental outlook.<\/p>\n<p align=\"left\">Another fundamental is working with strings in Windows PowerShell. In the past (like back in the VBScript days), I hated working with strings. I could do it, but it did not seem like much fun to me. I would rather pull weeds out of the garden than spend a day manipulating VBScript strings. Luckily, Windows PowerShell makes that a bit easier to handle.<\/p>\n<h2 align=\"left\">A string is an object<\/h2>\n<p align=\"left\">By now, you have probably heard that everything in Windows PowerShell is an object. This is one reason why the <b>Get-Member<\/b> Windows PowerShell cmdlet is so very important. It provides insight into the objects with which one might be confronted. Here is an example of piping a string object to <b>Get-Member<\/b> and looking at the methods that are available:<\/p>\n<p align=\"left\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-14-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-14-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2 align=\"left\">String objects<\/h2>\n<p align=\"left\">String objects have a single property. Properties describe things. Therefore, I can use the available string property to describe the string to me. What if I want to describe what am I most interested in? In this example, I am interested in how long is the string. So the property I have available to me is <b>Length<\/b> as shown here:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;string&quot;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a | Get-Member -MemberType property -Force<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp; TypeName: System.String<\/p>\n<p align=\"left\">&nbsp;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">Name&nbsp;&nbsp; MemberType Definition<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p align=\"left\" style=\"margin-left:30px\">Length Property&nbsp;&nbsp; int Length {get;}<\/p>\n<p align=\"left\">&nbsp;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a.Length<\/p>\n<p align=\"left\" style=\"margin-left:30px\">6<\/p>\n<h2 align=\"left\">Strings have lots of methods<\/h2>\n<p align=\"left\">Windows PowerShell strings have lots of methods. Again, I can use the Windows PowerShell <b>Get-Member<\/b> cmdlet to list the string methods. These are shown here:<\/p>\n<p align=\"left\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-14-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-14-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p align=\"left\">The string object is well documented on MSDN (for more information, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.string(v=vs.110).aspx\" target=\"_blank\">String Class<\/a>), and MSDN is the primary source of documentation for all the methods and other members of the objects. Most of the methods are really easy to use. For example, to find the position of a particular letter within a string, I use the <b>IndexOf<\/b> method. Here is an example of that technique:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;strings&quot;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a.IndexOf(&quot;s&quot;)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">0<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PS C:\\&gt; $a = &quot;strings&quot;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a.IndexOf(&quot;s&quot;)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">0<\/p>\n<p align=\"left\"><b>&nbsp; &nbsp; Note&nbsp; <\/b>The&nbsp;<b>IndexOf<\/b> method is zero based. This means that it begins counting at zero. The <b>Length<\/b> property is 1 based, <br \/>&nbsp; &nbsp; so it begins counting with 1.<\/p>\n<p align=\"left\">The <b>IndexOf<\/b> method has a couple of overloads. This means that the same method can be used in different ways. As I showed previously, the basic way to use <b>IndexOf<\/b> is to tell it to look for a letter. When it does this, it begins looking at the first position (index 0). When it finds a match, it is done.<\/p>\n<p align=\"left\">The second way to use <b>IndexOf<\/b> is to tell it what to look for and where to begin looking. In this example, the word <i>strings <\/i>is seven characters long. The first position that contains the letter &ldquo;s&rdquo; is in position zero. Therefore, if I tell it to begin looking for the letter &ldquo;s&rdquo; beginning with the second letter (index position 1), it will find the letter &ldquo;s&rdquo; in index 6 (the seventh position in our word). This is shown here:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;strings&quot;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a.IndexOf(&quot;s&quot;,1)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">6<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p align=\"left\">There is another way to use the <b>IndexOf<\/b> method that combines the first two ways.<\/p>\n<p align=\"left\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6012.Bubble.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6012.Bubble.png\" alt=\" Image of text bubble\" title=\"Image of text bubble\" \/><\/a><\/p>\n<p align=\"left\" style=\"margin-left:150px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/dr_scripto_anim.gif\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/dr_scripto_anim.gif\" alt=\" Image of Dr. Scripto\" width=\"143\" height=\"199\" title=\" Image of Dr. Scripto\" \/><\/a><\/p>\n<p align=\"left\"><span style=\"font-size:12px\">The last way I want to look at using the <\/span><b style=\"font-size:12px\">IndexOf<\/b><span style=\"font-size:12px\"> method today permits me to specify the letter I want to look for, the place I want to begin looking, and the number of characters that I want to search. After creating my string, I use the <\/span><b style=\"font-size:12px\">IndexOf<\/b><span style=\"font-size:12px\"> method to look for the letter &quot;s&quot; beginning in position 1. (Remember this is the second letter in the string; and therefore, I miss the initial letter &quot;s&quot;). I tell it to seek from position 1 to position 5 (the second letter through the sixth letter, which in this case is the letter &quot;g&quot;). It returns -1. When the <\/span><b style=\"font-size:12px\">IndexOf<\/b><span style=\"font-size:12px\"> method returns the number -1, it means that it did not find a match.<\/span><\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a = &quot;strings&quot;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a.IndexOf(&quot;s&quot;,1,5)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">-1<\/p>\n<p align=\"left\">The next time, I use the <b>IndexOf<\/b> method, I again specify the letter &quot;s,&quot; and I again begin searching at the second letter (index 1). But this time, I search through to the seventh letter (index 6). This time, the <b>IndexOf<\/b> method returns 6, which means it found a match in the sixth index position. This is shown here:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt; $a.IndexOf(&quot;s&quot;,1,6)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">6<\/p>\n<p align=\"left\" style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p align=\"left\"><b>&nbsp; &nbsp; Note&nbsp;<\/b> According to MSDN, it appears that there are at least 9 ways to use the <b>IndexOf<\/b> method.<\/p>\n<p align=\"left\">There is your simple introduction to using Windows PowerShell to work with strings. String Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p align=\"left\">I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">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 align=\"left\"><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 Windows PowerShell to work with strings. Microsoft Scripting Guy, Ed Wilson, is here. This has been an exciting week around the Scripting Household. From our lanai, we can see several little rabbits hopping around&mdash;they seem to like the clover. We have seen some butterflies (which in [&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-1010","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 Windows PowerShell to work with strings. Microsoft Scripting Guy, Ed Wilson, is here. This has been an exciting week around the Scripting Household. From our lanai, we can see several little rabbits hopping around&mdash;they seem to like the clover. We have seen some butterflies (which in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1010","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=1010"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1010\/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=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}