Trimming Strings (PowerShell)


To totally unlock this section you need to Log-in


Login

One of the most fundamental rules for working with data is “garbage in, garbage out”. 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 “raw data” 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 String methods, we can easily correct this situation.

The System.String .NET Framework class (which is documented on MSDN) has four Trim methods that enable me to easily cleanup strings by removing unwanted characters and white space. The following table lists these methods.

  • Trim(): Removes all leading and trailing white-space characters from the current String object.
  • Trim(Char[]): Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.
  • TrimEnd: Removes all trailing occurrences of a set of characters specified in an array from the current String object.
  • TrimStart: Removes all leading occurrences of a set of characters specified in an array from the current String object.

Trim white space from both ends of a string

The easiest Trim method to use is the Trim() method. It is very useful, and it is the method we use most. It easily removes all white space characters from the beginning and from the end of a string. This is shown here:

PS C:\> $string = " a String "
PS C:\> $string.Trim()
a String

The method is that easy to use. We just call Trim() 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, we obtain the length of the string before we trim it, and we save the resulting string following the trim operation back into a variable. We then obtain the length of the string a second time. Here is the command:

$string = " a String "
$string.Length
$string = $string.Trim()
$string
$string.Length

The command and the associated output from the command are shown in the following image:

Trimming Strings (PowerShell)

Trim specific characters

If there are specific characters we need to remove from both ends of a string, We can use the Trim(char[]) method. This permits me to specify an array of characters to remove from both ends of the string. Here is an example in which we have a string that begins with “a “ and ends with “ a”. We use an array consisting of “a”, “ “ and it removes both ends of the string. Here is the command:

$string = "a String a"
$string1 = $string.Trim("a"," ")

The command and its associated output are shown in the image that follows:

Trimming Strings (PowerShell)

The cool thing is that we can also specify Unicode in my array of characters. This technique is shown here:

$string = "a String a"
$string1 = $string.Trim([char]0x0061, [char]0x0020)

The following image illustrates this technique by displaying the command and the associated output from that command:

Trimming Strings (PowerShell)

Trim end characters

There are times when we know specifically that we need to trim characters from the end of a string. However, those characters might also be present at the beginning of the string, and we need to keep those characters. For these types of situations, we use the TrimEnd method. The cool thing is that we can automatically use this method three ways:

  • We can delete a specific character if I type that specific Unicode character code.
  • We can delete a specific character if I type that specific character.
  • We can delete Unicode white-space characters if I do nothing but call the method.

Delete a specific character

In this example, we 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:

$string = "a String a"

We now specify two Unicode characters by code value to delete from the end of the string. We store the returned string in a variable as shown here:

$string1 = $string.TrimEnd([char]0x0061, [char]0x0020)

The command and the associated output are shown in the following image:

Trimming Strings (PowerShell)

We can also specify the specific character to trim by typing the characters. In this example, we type the <space> a characters as a single array element, so it will only delete <space> a from the end of the string:

PS C:\> $string = "a String a"
PS C:\> $string.Length
10
PS C:\> $string1 = $string.TrimEnd(" a")
PS C:\> $string1
a String
PS C:\> $string1.Length
8

In the following example, We specify the characters as individual elements in an array. In fact, we do not even have them in the same order as they appear in the string. Yet, the results are the same.

PS C:\> $string = "a String a"
PS C:\> $string1 = $string.TrimEnd("a", " ")
PS C:\> $string1.Length
8
PS C:\> $string1
a String
PS C:\>

Delete white space

If we do not specify any characters, the TrimEnd 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 we trim the end, the length is only 18 characters long, and both the space and the tab are gone. This technique is shown here:

PS C:\> $string = "a string and a tab `t"
PS C:\> $string1 = $string.TrimEnd()
PS C:\> $string.Length
20
PS C:\> $string1
a string and a tab
PS C:\> $string1.length
18

Trim start characters

If we need to trim stuff from the beginning of a string, We use the TrimStart method. It works exactly the same as the TrimEnd method. So it will also work in three ways as follow:

  • Delete a specific character from beginning of string if we type that specific Unicode character code.
  • Delete a specific character from beginning of string if we type that specific character.
  • Delete Unicode white-space characters from beginning of string if we do nothing but call the method.

Delete specific characters from start

Like the TrimEnd method, we can specify Unicode characters by Unicode code value. When we do this, the TrimStart method deletes those characters from the beginning of a string. This technique is shown here:

PS C:\> $string = "a String a"
PS C:\> $string.Length
10
PS C:\> $string1 = $string.TrimStart([char]0x0061, [char]0x0020)
PS C:\> $string1.Length
8
PS C:\> $string1
String a

Instead of using the Unicode code values, we can simply type the array of string characters we want to delete.

Note: One disadvantage of typing specific characters, is that “ “ 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, we know exactly what is meant, and therefore the script is more specific.

In this example, we type specific characters that need to be removed from the beginning of the string:

PS C:\> $string = "a String a"
PS C:\> $string.Length
10
PS C:\> $string1 = $string.TrimStart(" ", "a")
PS C:\> $string1.Length
8
PS C:\> $string1
String a
PS C:\>

Delete white space

If we call the TrimStart 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:

PS C:\> $string = "   String a"
PS C:\> $string.Length
11
PS C:\> $string1 = $string.TrimStart()
PS C:\> $string1.Length
8
PS C:\> $string1
String a
PS C:\>

That is all there is to using String methods.