Two Simple PowerShell Methods to Remove the Last Letter of a String

Two Simple PowerShell Methods to Remove the Last Letter of a String

  • Comments 9
  • Likes

Summary: Learn two simple Windows PowerShell methods to remove the last letter of a string.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! I have, what should be a simple question. I need to be able to remove only the last character from a string. For example, if I have a string the scripts, I want the string to be the script instead. If I have the number 12345, I want the number to be 1234. I have tried everything, including split, trim, and other commands, but nothing seems to work. I thought that substring would work, but it requires me to know the length of the string, and that is something I will not always know. Can you help me?

—DM

 

Hey, Scripting Guy! AnswerHello DM,

Microsoft Scripting Guy Ed Wilson here. In the VBScript days, I used to hate string manipulation. It was not that the power of VBScript’s string manipulation language was so weak; it was simply that I did not like to monkey around with strings. Therefore, one day, I spent an entire day doing string manipulation. After that, I was a lot better, and I did not hate it so much. The reason I did not like string manipulation was that I was not that good at it. In Windows PowerShell, string manipulation is still string manipulation. The tools and the commands are similar to what I had in the VBScript days, and indeed to what is available in other languages.

Nearly everything has a concept of split, trim, substring, and other similar-sounding commands. The commands may not be called exactly the same thing, but they will offer similar functionality. In Windows PowerShell, we are actually using the methods from the string class that is found in the System .NET Framework namespace. This is good news, because the string class is very robust and complete. If you long for the actual commands used back in the VBScript days, those are available via the Microsoft.VisualBasic namespace.

DM, you mentioned the need to know the length of a string. As a matter of a fact, all strings have a length property associated with them. To find the properties of a string, I can use the Get-Member cmdlet:

“a string” | get-member –membertype property

The command and associated output are shown in the following figure.

Image of command and associated output

Because I have found a length property on the string class, I can use it to obtain the length of a string. If I store a string in a variable, I can access the length property directly, as shown here:

PS C:\> $string = "the scripts"

PS C:\> $string.Length

11

One thing to keep in mind when working with the length property is that it reports the actual length of the string. That is, it is not zero based. The following code illustrates this:

PS C:\> "123456789".length

9

Though it is common to store a string in a variable and use dotted notation to retrieve the length property, the length property is also directly accessible from a string. Tab expansion does not pick it up, but it is available nonetheless. This technique is shown here:

PS C:\> "The scripts".length

11

The preceding commands and associated output are shown in the following figure.

Image of preceding commands and associated output

DM, there are two ways to use the substring method from the string class. The first way to use the substring method defines the start location, and the method returns the remainder of the string. The second way is to specify the start location and to tell the substring method how many letters to return. I found this information by using the Get-Member cmdlet, and choosing the definition of the substring method. The command and returned information are shown here:

PS C:\> $string | gm substring | select definition

 

Definition

----------

string Substring(int startIndex), string Substring(int startIndex, int length)

 

First, I want to show what happens when I specify a value for only the startindex parameter of the substring method. In the first example, I tell substring to begin at position 1 (the letter t in the string the scripts) and return the remainder of the string. The command and output are shown here:

PS C:\> $string = "the scripts"

PS C:\> $string.Substring(1)

he scripts

 

In the second command, I tell the substring method to begin at position 10 and to return the remainder of the string. Because the string is 11 characters long, the command only returns the letter s. The command and associated output are shown here:

PS C:\> $string.Substring(10)

s

PS C:\>

Because the length property always exists on a string, I can use it with the substring method. This is useful with the second position of the command, because I do not always know how long the string is. To begin with the first position of the string, I need to specify location 0 (remember when I used location 1, the first character returned was the letter h). If I simply use the length property, I will return the entire string. This is shown here:

PS C:\> $string = "the scripts"

PS C:\> $string.Substring(0,$string.Length)

DM, you need to return all of a string but the last letter. To do this, I simply subtract 1 from the length of the string, as shown here:

PS C:\> $string = "the scripts"

PS C:\> $string.Substring(0,$string.Length-1)

the script

This is simply displaying the string minus the last letter of the string. To actually remove the last letter from the string, it is necessary to write the results back to the $string variable. This technique is shown here:

PS C:\> $string = "the scripts"

PS C:\> $string = $string.Substring(0,$string.Length-1)

PS C:\> $string

the script

PS C:\>

Another way to accomplish this, which might actually be a bit easier to do, is to use the replace operator and not supply a replacement value. The replace operator will accept regular expressions. A simple regular expression of “.$” will match the last character in a string. The dollar sign means match from the end of the string, and a period (or dot) means any single character. Therefore, the following command will replace the last character with nothing, and effectively remove the last letter of the string:

PS C:\> $string = "the scripts"

PS C:\> $string -replace ".$"

the script 

Once again, if I want to actually remove one character from the end of a string, I need to write the returned string back to the $string variable, as shown here:

PS C:\> $string = "the scripts"

PS C:\> $string = $string -replace ".$"

PS C:\> $string

the script 

One of the cool things about Windows PowerShell is there are multiple ways of doing the same thing. I have illustrated two methods of removing the last character of a string. For comparison’s sake, both techniques and associated output are shown in the following figure.

Image of both techniques and associated output

 

DM, that is all there is to removing the last letter of a string. I invite you to join me tomorrow for more exciting Windows PowerShell goodness.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>Hi Ed,</p> <p>these are the best solutions to the problem!</p> <p>I recently had a similar problem and I&#39;m still wondering why we can&#39;t have an </p> <p>extension to the range operator like other scripting languages have and even</p> <p>CMD.EXE has this one:</p> <p>C:\&gt;set string=the scripts</p> <p>C:\&gt;echo %string:~0,-1%</p> <p>the script</p> <p>The idea is to allow for ranges that may anchor to the end of the string.</p> <p>Instead of </p> <p>$string.Substring(0,$string.Length-1)</p> <p>I would like to have</p> <p>$string.Substring(0,-1)</p> <p>or if we think of arrays slices, we already have:</p> <p>$string.ToCharArray()[0..9]</p> <p>but might want to have something like:</p> <p>$string.ToCharArray()[0..-1]</p> <p>Maybe the powershell team will surprise us with that sometime ???</p> <p>Klaus.</p>

  • <p>Too cool the Regex!</p>

  • <p>@Klaus Thanks. I am glad you enjoyed the article. You can always request extensions to Windows PowerShell via the connect site. The PowerShell team pays attention to these. </p> <p>@compugab Thanks</p>

  • <p>I really like your blog and want to thank you for spending the time and effort on doing it.</p> <p>It&#39;s genuinely excellent.</p>

  • <p>@Richard Awesome! Comments like this are one reason I keep at it. I am so glad that you find the content useful, and that you enjoy the Hey Scripting Guy! blog. Thank you so much for the kind words.</p>

  • <p>This is also a simple way that is a bit easier to read.</p> <p>$string = $string.remove($string.length - 1)</p> <p>String.Remove uses a zero-based index thus the &quot;-1&quot;.</p>

  • <p>I have a csv file that I&#39;m importing to extract information from each valid record. &nbsp;My problem is the last record has a special ascii character in it telling me it&#39;s the last record in the file. &nbsp;This record gets added during an ftp process that I can&#39;t control.</p> <p>Is there a way, during the import-csv cmdlt to check the length of each record and if it is zero or say &lt; 10 in size to bypass it?</p>

  • <p>@ El Bee Newbie </p> <p>Funny you should ask. &nbsp;This is such an old post.</p> <p>The short answer is - Yes!</p> <p>Start here: &nbsp;<a rel="nofollow" target="_new" href="http://social.technet.microsoft.com/Forums/en-US/ITCG/threads">social.technet.microsoft.com/.../threads</a></p>

  • <p>THANKS so much for posting this! &nbsp;I was able to resolve my issue of trimming off the unwanted millisecond values from TimeSpan in a flash. &nbsp;(in a millisecond?) &nbsp;:-)</p>