You are reading the article How Trim Function Works In Powershell updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Trim Function Works In Powershell
Introduction to PowerShell TrimPowerShell 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.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
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:
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 TrimGiven 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.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:
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:
29
After removing whitespace.
Code:
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.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.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.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.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.Trim([char]0x0061)
Output:
You can find the list of Unicode characters in the below link.
The above example is similar to.
Code:
$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.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.TrimStart(‘P’)
Output:
If no character is specified inside, it will consider whitespace as a character and remove eliminate the whitespace at the beginning.
$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.TrimStart(“PowerShell”)
Output:
Note: TrimStart() operation is also case sensitive, and we can use Unicode characters by converting them into character datatype for this operation.
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.TrimEnd()
Output:
Once we specify any character inside this method, it will remove the trailing character from the string.
Code:
Output:
If there are multiple same characters specified at the end, it will remove all trailing same characters.
Code:
$str.TrimEnd(‘d’)
Output:
We can also eliminate multiple trailing characters.
Code:
$str.TrimEnd(“Hello”)
Output:
Note: This operation is case-sensitive, and you can also use Unicode characters as shown in the earlier example to trim the string.
ConclusionTrim 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 ArticlesThis 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 –
PowerShell Sleep
PowerShell SubString
PowerShell not like
Else If in PowerShell
You're reading How Trim Function Works In Powershell
Update the detailed information about How Trim Function Works In Powershell on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!