Powershell provides a lot of different string manipulation functions and features.
Trim function is used to remove specified characters from string with different ways. In this tutorial we will look some of the trim examples.
Trim White Spaces From Start and End
We will start by trimming given string or texts beginning and end white spaces. This operation generally done when a string operation is finished like extraction and be sure that string is clear and there is no unwanted characters.
$ " This is my dirty text ".trim()
Trim Specific Characters
In some situations we may want to clear the text from specific characters. We only want to delete them. For example if we do not like
s character 😉 we can remove this character from the string by giving
s as parameter to the trim function.
Remove Characters From Begging
We can delete characters from beginning of the text y specifying characters. We will use
trimstart function and provide the characters. In this examples we will delete
T character from start of the text.
$ "This is my dirty text".Trimstart("T")
Remove Characters From End
In previous step we have removed characters from start of the text. We can also delete characters from end of the text. We will use
trimend function. We will remove the
t character from end of the string.
$ "This is my dirty text".Trimend("t")