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.
Powershell提供了许多不同的字符串处理功能和特性。 Trim
功能用于以不同方式从字符串中删除指定的字符。 在本教程中,我们将看一些修剪示例。
从开始和结束修剪空白 (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.
在某些情况下,我们可能希望清除特定字符中的文本。 我们只想删除它们。 例如,如果我们不喜欢s
字符😉,则可以通过将s
作为参数给trim函数将其从字符串中删除。
从乞讨中删除字符 (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.
我们可以从指定字符的文本y的开头删除字符。 我们将使用trimstart
功能并提供字符。 在此示例中,我们将从文本开头删除T
字符。
$ "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.
在上一步中,我们从文本开头删除了字符。 我们还可以从文本末尾删除字符。 我们将使用trimend
函数。 我们将从字符串末尾删除t
字符。
$ "This is my dirty text".Trimend("t")
翻译自: https://www.poftut.com/powershell-trim-operation-like-filename-string-end-start-spaces/