Updated March 6, 2023
Introduction to PowerShell Trim
PowerShell Trim() methods (Trim(), TrimStart() and TrimEnd()) are used to remove the leading and trailing white spaces and the unwanted characters from the string or the raw data like CSV file, XML file, or a Text file that can be converted to the string and returns the new string. These methods are part of the System.String .Net Class, and once these methods are applied, it produces the new string instead of manipulating the current string.
Syntax:
- Trim(): Trims (Removes) all the leading and trailing white space characters from the current string object.
- Trim(Char[]): It removes all the leading and trailing characters from the current string object.
- TrimeStart: It removes all occurrences of a character from the beginning of the current string object.
- TrimEnd: It removes all occurrences of a character from the end of the current string object.
How does Trim Function work in PowerShell?
Trim() methods are System.String .NET class methods and used to eliminate the leading and/or trailing characters, including whitespace from the string. They are only available in the string class.
Code:
$str = " This is a PowerShell String "
$str | gm
Output:
In the above output, all Trim() methods return a string that is the new string after the operation, and that is why we need to store it to the new variable if it requires further operation on it. Other datatypes don’t support Trim() methods.
Examples of PowerShell Trim
Given below are the examples of PowerShell Trim:
Example #1 – Trim() method.
This function will remove the leading and trailing whitespaces from the string object, as shown below.
Code:
$str = " This is a PowerShell String "
$str.Trim()
Output:
You might have noticed that it generates a new string. We can save this string to the new variable for later use.
Code:
$str = " This is a PowerShell String "
$str1 = $str.Trim()
$str1
Output:
You can also directly pass a string instead of a variable and operate on it.
Code:
(" This is a PowerShell String ").Trim()
Output:
It is just removing spaces, so we will check the output length before and after trimming whitespaces because trimmed whitespaces may not be visible in the output.
Code:
$str = " This is a PowerShell String "
$str.Length
29
After removing whitespace.
Code:
$str1 = $str.Trim()
$str1.Length
27
Example #2 – Trim(Char) method to trim specific character.
Suppose we have the below string to trim its starting and ending character.
Code:
$str = "aThis is a PowerShell String"
$str.Trim('a')
Output:
Here we have provided a character ‘a’ to trim, and in the string, ‘a’ is at the start of the string but not at the end of the script. So it removes only the start character. If the ‘a’ is at both ends, then the leading and trailing character ‘a’ will be removed.
Code:
$str = "aThis is a PowerShell Stringa"
$str.Trim('a')
Output:
Below is also the same operation.
Code:
$str.Trim("a"," ")
Output:
The question is, what if we have the leading or trailing space and if we provide the first character to remove.
Code:
$str = " aThis is a PowerShell String"
$str.Trim('a')
Output:
The command won’t perform any action on the string because its leading character is whitespace.
Please note trimming the specific character is a case sensitive operation.
Code:
$str = "aThis is string trimming"
$str.Trim('A')
Output:
Code:
$str.Trim('G')
Output:
Example #3 – Trim (Unicode) method to trim specific character.
The good thing about PowerShell is that we can also pass the Unicode character and then type conversion to character data to trim leading and trailing characters.
Code:
$str = "aThis is string trimming"
$str.Trim([char]0x0061)
Output:
You can find the list of Unicode characters in the below link.
https://en.wikipedia.org/wiki/List_of_Unicode_characters.
The above example is similar to.
Code:
$str = "aThis is string trimming"
$str.Trim([char]0x0061,[char]0x0020)
Output:
Here 0x0061 represents ‘a’ and 0x0020 represents whitespace.
Example #4 – Trim (Char[]) method to remove leading and trailing space.
In the above examples, we have used a single character that was removed from the start or the end of the string if it was matched. If we provide multiple characters, it will remove them from the leading and trailing characters. An explanation can be understood with the examples below.
Code:
("hello world").Trim('hd')
Output:
We have provided two characters, ‘hd’, and it will check both the characters at the start and end of the string, and if matched, it will remove them.
Code:
("dhello world").Trim('hd')
Output:
From the above example, we can see that the character sequence doesn’t matter.
You can also remove the entire word using this method.
Code:
$str = "PowerShell hello PowerShell"
$str.Trim('PowerShell')
Output:
Example #5 – TrimStart(Char) method.
In the Trim() method, we can delete leading and trailing characters while TrimStart() delete only leading character(s), as shown in the example below.
Code:
$str = "PowerShell helloP"
$str.TrimStart('P')
Output:
If no character is specified inside, it will consider whitespace as a character and remove eliminate the whitespace at the beginning.
Code:
$str = " Hello World "
$str.TrimStart()
Output:
In this example, trailing whitespace is note removed; we can check it by counting the number of words.
Example #6 – TrimStart(Char[]) method.
We can also use the multiple leading characters to remove from the string.
Code:
$str = "PowerShell hello PowerShell"
$str.TrimStart("PowerShell")
Output:
Example #7 – TrimEnd() method.
We can use TrimEnd() method to remove trailing characters.
If we don’t specify anything, it will remove the ending whitespace but not the trailing whitespace.
Code:
$str = " Hello World "
$str.TrimEnd()
Output:
Once we specify any character inside this method, it will remove the trailing character from the string.
Code:
PS C:\> $str = "dHello World"
PS C:\> $str.TrimEnd('d')
Output:
If there are multiple same characters specified at the end, it will remove all trailing same characters.
Code:
$str = "dHello Wordd"
$str.TrimEnd('d')
Output:
We can also eliminate multiple trailing characters.
Code:
$str = "Hello PowerShell Hello"
$str.TrimEnd("Hello")
Output:
Conclusion
Trim functions are very helpful for the string operations, especially when we are working with the text file or the CSV file and we need to remove some entries from the string.
Recommended Articles
This is a guide to PowerShell Trim. Here we discuss the introduction, how to trim function works in PowerShell? and examples. You may also have a look at the following articles to learn more –