{"id":985,"date":"2014-07-18T00:01:00","date_gmt":"2014-07-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/18\/trim-your-strings-with-powershell\/"},"modified":"2014-07-18T00:01:00","modified_gmt":"2014-07-18T00:01:00","slug":"trim-your-strings-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/trim-your-strings-with-powershell\/","title":{"rendered":"Trim Your Strings with PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to trim strings and clean up data.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife heads out today to spend time with her other passion at the <a href=\"http:\/\/www.blueridgeclassic.com\/\" target=\"_blank\">Blue Ridge Classic Horse Show<\/a>. Unfortunately, I will not get to attend much of that event due to a week of training that my group is doing. But I will have the chance to see at least one day of the events.<\/p>\n<h3>PowerShell Summit Europe<\/h3>\n<p>Speaking of the Scripting Wife&rsquo;s passions, registration is open now for the <a href=\"http:\/\/powershell.org\/wp\/community-events\/summit\/\" target=\"_blank\">Windows PowerShell Summit in Europe<\/a>. The event will be held Sept 29 &ndash; Oct 1, 2014 in Amsterdam in The Netherlands. This will be an awesome event, and it will be a great chance to meet other PowerShellers. Teresa and I were in Amsterdam earlier this year. The city is beautiful, and the people were really friendly.<\/p>\n<h2>Trim strings using String methods<\/h2>\n<p><b>&nbsp; &nbsp; Note<\/b>&nbsp;&nbsp;This post is the last in a series about strings. You might also enjoy reading:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-string-theory\/\" target=\"_blank\">PowerShell String Theory<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/keep-your-hands-clean-use-powershell-to-glue-strings-together\/\" target=\"_blank\">Keep Your Hands Clean: Use PowerShell to Glue Strings Together<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/join-me-in-a-few-string-methods-using-powershell\/\" target=\"_blank\">Join Me in a Few String Methods Using PowerShell<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-the-split-method-in-powershell\/\" target=\"_blank\">Using the Split Method in PowerShell<\/a><\/li>\n<\/ul>\n<p>One of the most fundamental rules for working with data is &ldquo;garbage in, garbage out.&rdquo; This means it is important to groom data before persisting it. It does not matter if the persisted storage is Active Directory, SQL Server, or a simple CSV file. One of the problems with &ldquo;raw data&rdquo; is that it may include stuff like leading spaces or trailing spaces that can affect sort and search routines. Luckily, by using Windows PowerShell and a few <b>String<\/b> methods, I can easily correct this situation.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.string(v=vs.110).aspx\" target=\"_blank\">System.String<\/a> .NET Framework class (which is documented on MSDN) has four <b>Trim<\/b> methods that enable me to easily cleanup strings by removing unwanted characters and white space. The following table lists these methods.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"103\" valign=\"top\">\n<p><b>Method<\/b><\/p>\n<\/td>\n<td width=\"535\" valign=\"top\">\n<p><b>Meaning<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"103\" valign=\"top\">\n<p>Trim()<\/p>\n<\/td>\n<td width=\"535\" valign=\"top\">\n<p>Removes all leading and trailing white-space characters from the current String object.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"103\" valign=\"top\">\n<p>Trim(Char[])<\/p>\n<\/td>\n<td width=\"535\" valign=\"top\">\n<p>Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"103\" valign=\"top\">\n<p>TrimEnd<\/p>\n<\/td>\n<td width=\"535\" valign=\"top\">\n<p>Removes all trailing occurrences of a set of characters specified in an array from the current String object.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"103\" valign=\"top\">\n<p>TrimStart<\/p>\n<\/td>\n<td width=\"535\" valign=\"top\">\n<p>Removes all leading occurrences of a set of characters specified in an array from the current String object.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"font-size:1.5em\">Trim white space from both ends of a string<\/span><\/h2>\n<p>The easiest <b>Trim<\/b> method to use is the <b>Trim()<\/b> method. It is very useful, and it is the method I use most. It easily removes all white space characters from the beginning and from the end of a string. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string = &quot; a String &quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string.Trim()<\/p>\n<p style=\"margin-left:30px\">a String<\/p>\n<p>The method is that easy to use. I just call <b>Trim()<\/b> on any string, and it will clean it up. Unfortunately, the previous output is a bit hard to understand, so let me try a different approach. This time, I obtain the length of the string before I trim it, and I save the resulting string following the trim operation back into a variable. I then obtain the length of the string a second time. Here is the command:<\/p>\n<p style=\"margin-left:30px\">$string = &quot; a String &quot;<\/p>\n<p style=\"margin-left:30px\">$string.Length<\/p>\n<p style=\"margin-left:30px\">$string = $string.Trim()<\/p>\n<p style=\"margin-left:30px\">$string<\/p>\n<p style=\"margin-left:30px\">$string.Length<\/p>\n<p>The command and the associated output from the command are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Trim specific characters<\/h2>\n<p>If there are specific characters I need to remove from both ends of a string, I can use the <b>Trim(char[])<\/b><i> <\/i>method. This permits me to specify an array of characters to remove from both ends of the string. Here is an example in which I have a string that begins with &ldquo;a &ldquo; and ends with<strong> &ldquo; a&rdquo;<\/strong>. I use an array consisting of <strong>&ldquo;a&rdquo;, &ldquo; &ldquo;<\/strong> and it removes both ends of the string. Here is the command:<\/p>\n<p style=\"margin-left:30px\">$string = &quot;a String a&quot;<\/p>\n<p style=\"margin-left:30px\">$string1 = $string.Trim(&quot;a&quot;,&quot; &quot;)<\/p>\n<p>The command and its associated output are shown in the image that follows:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The cool thing is that I can also specify Unicode in my array of characters. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">$string = &quot;a String a&quot;<\/p>\n<p style=\"margin-left:30px\">$string1 = $string.Trim([char]0x0061, [char]0x0020)<\/p>\n<p><b>&nbsp;Dr. Scripto says:&nbsp;<\/b><\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &ldquo;Convenient <a href=\"http:\/\/en.wikipedia.org\/wiki\/List_of_Unicode_characters\" target=\"_blank\">Unicode tables<\/a> are available on Wikipedia.&rdquo;<\/p>\n<p style=\"margin-left:60px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4380.dr_scripto_anim.gif\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4380.dr_scripto_anim.gif\" alt=\"Image of Dr. Scripto\" title=\"Image of Dr. Scripto\" \/><\/a><\/p>\n<p>The following image illustrates this technique by displaying the command and the associated output from that command:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Trim end characters<\/h2>\n<p>There are times when I know specifically that I need to trim characters from the end of a string. However, those characters might also be present at the beginning of the string, and I need to keep those characters. For these types of situations, I use the <b>TrimEnd<\/b><i> <\/i>method. The cool thing is that I can automatically use this method three ways:<\/p>\n<ol>\n<li>I can delete a specific character if I type that specific Unicode character code.<\/li>\n<li>I can delete a specific character if I type that specific character.<\/li>\n<li>I can delete Unicode white-space characters if I do nothing but call the method.<\/li>\n<\/ol>\n<h3>Delete a specific character<\/h3>\n<p>In this example, I create a string and then specify an array of specific Unicode characters by using the Unicode code value. Because the string begins with the same characters that it ends with, this is a good test to show how I can delete specific characters from the end of the string. Here is the string:<\/p>\n<p style=\"margin-left:30px\">$string = &quot;a String a&quot;<\/p>\n<p>I now specify two Unicode characters by code value to delete from the end of the string. I store the returned string in a variable as shown here:<\/p>\n<p style=\"margin-left:30px\">$string1 = $string.TrimEnd([char]0x0061, [char]0x0020)<\/p>\n<p>The command and the associated output are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-18-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can also specify the specific character to trim by typing the characters. In this example, I type the<b>&lt; space&gt; a<\/b> characters as a single array element, so it will only delete <b>&lt;space&gt; a<\/b> from the end of the string:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string = &quot;a String a&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string.Length<\/p>\n<p style=\"margin-left:30px\">10<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1 = $string.TrimEnd(&quot; a&quot;)<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1<\/p>\n<p style=\"margin-left:30px\">a String<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1.Length<\/p>\n<p style=\"margin-left:30px\">8<\/p>\n<p>In the following example, I specify the characters as individual elements in an array. In fact, I do not even have them in the same order as they appear in the string. Yet, the results are the same.<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string = &quot;a String a&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1 = $string.TrimEnd(&quot;a&quot;, &quot; &quot;)<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1.Length<\/p>\n<p style=\"margin-left:30px\">8<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1<\/p>\n<p style=\"margin-left:30px\">a String<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h3>Delete white space<\/h3>\n<p>If I do not specify any characters, the <b>TrimEnd<\/b><i> <\/i>method automatically deletes all Unicode white-space characters from the end of the string. In this example, a string contains both a space and a tab character at the end of the string. The length is 20 characters long. After I trim the end, the length is only 18 characters long, and both the space and the tab are gone. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string = &quot;a string and a tab `t&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1 = $string.TrimEnd()<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string.Length<\/p>\n<p style=\"margin-left:30px\">20<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1<\/p>\n<p style=\"margin-left:30px\">a string and a tab<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1.length<\/p>\n<p style=\"margin-left:30px\">18<\/p>\n<h2>Trim start characters<\/h2>\n<p>If I need to trim stuff from the beginning of a string, I use the <b>TrimStart<\/b><i> <\/i>method. It works exactly the same as the <b>TrimEnd<\/b><i> <\/i>method. So it will also work in three ways as follow:<\/p>\n<ol>\n<li>Delete a specific character from beginning of string if I type that specific Unicode character code.<\/li>\n<li>Delete a specific character from beginning of string if I type that specific character.<\/li>\n<li>Delete Unicode white-space characters from beginning of string if I do nothing but call the method.<\/li>\n<\/ol>\n<h3>Delete specific characters from start<\/h3>\n<p>Like the <b>TrimEnd<\/b><i> <\/i>method, I can specify Unicode characters by Unicode code value. When I do this, the <b>TrimStart<\/b><i> <\/i>method deletes those characters from the beginning of a string. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string = &quot;a String a&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string.Length<\/p>\n<p style=\"margin-left:30px\">10<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1 = $string.TrimStart([char]0x0061, [char]0x0020)<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1.Length<\/p>\n<p style=\"margin-left:30px\">8<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1<\/p>\n<p style=\"margin-left:30px\">String a<\/p>\n<p>Instead of using the Unicode code values, I can simply type the array of string characters I want to delete.<\/p>\n<p style=\"margin-left:30px\"><b>Note <\/b>&nbsp;One disadvantage of typing specific characters, is that <b>&ldquo; &ldquo;<\/b> is kind of hard to interpret. Is it a space, a tab, or a null value? Is it a typing error, or is it intentional? By using a specific Unicode code value, I know exactly what is meant, and therefore the script is more specific.<\/p>\n<p>In this example, I type specific characters that need to be removed from the beginning of the string:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string = &quot;a String a&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string.Length<\/p>\n<p style=\"margin-left:30px\">10<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1 = $string.TrimStart(&quot; &quot;, &quot;a&quot;)<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1.Length<\/p>\n<p style=\"margin-left:30px\">8<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1<\/p>\n<p style=\"margin-left:30px\">String a<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h3>Delete white space<\/h3>\n<p>If I call the <b>TrimStart<\/b><i> <\/i>method and do not supply specific characters, the method will simply delete all Unicode white space from the beginning of the string. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string = &quot;&nbsp;&nbsp; String a&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string.Length<\/p>\n<p style=\"margin-left:30px\">11<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1 = $string.TrimStart()<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1.Length<\/p>\n<p style=\"margin-left:30px\">8<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $string1<\/p>\n<p style=\"margin-left:30px\">String a<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>That is all there is to using <b>String<\/b> methods. This also concludes String Week. Join me tomorrow when I will talk about exploring the Windows PowerShell <b>$Profile<\/b> variable.<\/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\" 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><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 trim strings and clean up data. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife heads out today to spend time with her other passion at the Blue Ridge Classic Horse Show. Unfortunately, I will not get to attend much of that [&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-985","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 trim strings and clean up data. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife heads out today to spend time with her other passion at the Blue Ridge Classic Horse Show. Unfortunately, I will not get to attend much of that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/985","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=985"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/985\/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=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}