当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 进行 PowerShell Trim 的绝佳方法(操作指南)

作者:精品下载站 日期:2024-12-14 13:03:15 浏览:15 分类:玩电脑

进行 PowerShell Trim 的绝佳方法(操作指南)


数据很少是完美的。字符串在许多数据文件中广泛使用。通常,脚本需要修改字符串数据以删除内容开头或结尾的无关字符。幸运的是,我们有 PowerShell Trim() 方法!

PowerShell trim() 方法可以通过多种修剪字符串的方法(例如 Powershell trim() 和 Powershell trimend())轻松修剪这些字符方法。

在本文中,我们将探讨可用的各种技术,以及如何以您需要的格式获取字符串数据的示例。

相关:回归基础:PowerShell 字符串

先决条件

如果您想使用本教程中的技术,我们假设您拥有 PowerShell。本教程将使用 PowerShell 7 演示这些概念,但所有步骤也应适用于 Windows PowerShell 和早期版本的 PowerShell Core。

使用 PowerShell Trim() 方法

在 PowerShell 中修剪字符串的最常见方法之一是使用 trim() 方法。与 PowerShell 中的所有其他修剪方法一样,trim() 方法是 System.String .NET 类的成员。此方法允许您修剪字符串开头和结尾的所有空格或修剪某些字符。

trim() 方法区分大小写!

修剪空白

要从字符串中删除空格,只需调用不带参数的 trim() 方法,如下所示。

PowerShell Trim() 方法未提供任何参数时,将调用 Char.IsWhiteSpace 方法,并删除找到的所有前导和结尾空格字符。

# Expected to remove leading and trailing two spaces.
("  Test  ").Trim()

# Expected to remove the leading two spaces and carriage return and newline characters.
("  Test`r`n").Trim()

# Expected to remove the leading two spaces and trailing Unicode Next Line character.
("  Test Test $([Char]0x0085)").Trim()

[玩转系统] 进行 PowerShell Trim 的绝佳方法(操作指南)

.NET Framework 3.5 SP1 及更早版本使用与 .NET Framework 4 及更高版本不同的空白字符列表。这意味着在早期的 .NET 版本中,零宽度空格 (U+200B) 和零宽度无中断空格 (U+FEFF) 字符已被删除,而在更高版本的 .NET 版本中则没有。

此外,在 .NET 的早期版本中,Trim() 方法不会删除以下三个 Unicode 空白字符:MONGOLIAN VOWEL SEPARATOR (U+180E)、NARROW NO-BREAK SPACE (U+202F) 和 MEDIUM数学空间(U+205F)。这些在 .NET 的更高版本中被删除

修剪单个字符

如果您想从字符串中删除单个字符,可以使用 trim 方法来实现。此方法会删除所有实例中字符串开头和结尾的该字符,直到遇到与所提供的字符不同的字符为止。

看一下下面关于修剪字符 Tt 的示例。

# Expected to remove nothing as the leading character is a space and the ending is a lowercase "t".
(" Test Test").Trim("T")
# Expected to remove the leading "T" but not the ending lowercase "t".
("Test  Test").Trim("T")
# Expected to remove nothing as the leading "T" is uppercase and no ending "t".
("Test  String").Trim("t")
# Expected to remove both the leading and ending "t".
("test test").Trim("t")

[玩转系统] 进行 PowerShell Trim 的绝佳方法(操作指南)

修剪字符数组

也许您想从字符串中删除多个字符。没问题。您可以为 PowerShell trim() 方法提供字符数组。

trim() 方法提供一个字符数组将删除所有这些字符,直到遇到一个字符,而不是从字符串对象的开头和结尾开始的数组中。

trim() 方法接受字符数组,例如简单的字符串(例如 abc123)或适当的数组,例如 @("A"," B","C","1","2","3")

将数组与trim()方法一起使用可能会产生一些意想不到的结果。

# Expected to remove the leading and trailing "ABC123" string.
("ABC123TestABC123TestABC123").Trim("ABC123")
# Expected to remove the leading and trailing "ABC123" string.
("ABC123TestABC123TestABC123").Trim(@("A","B","C","1","2","3"))
# Expected to remove the leading "A1B2C3" string and trailing "1A2B3C" string.
("A1B2C3Test123TestABC123TestABCTest1A2B3C").Trim("ABC123")

正如您在下面所看到的,即使字符的放置顺序不正确,它们仍然会被删除,因为它们适合给定数组中的字符之一。如果您期望从字符串的开头和结尾删除确切的字符串,则使用此方法将不起作用。

[玩转系统] 进行 PowerShell Trim 的绝佳方法(操作指南)

使用 TrimEnd() 和 TrimStart()

如果您不想修剪字符串开头和结尾的字符,则可以使用更具体的 PowerShell TrimEnd()TrimStart 来进行选择() 方法。这些方法反映了 PowerShell Trim() 方法 的功能。

TrimEnd() 和 TrimStart() 方法区分大小写!

TrimStart()TrimEnd() 都可以让您更好地控制要处理字符串对象的哪个部分。根据具体情况,这些方法可能更适合您的数据需求。

看看下面的一些例子。

# Expected to only remove the leading two spaces and maintaining the trailing carriage return and new-line characters.
("  Test`r`n").TrimStart()
# Expected to remove the two leading "a" characters and leaving the trailing "a" characters.
("aaTestaa").TrimStart("a")
# Expected to remove the leading "abc123" and leave the trailing "abc123" characters.
("abc123Testabc123").TrimStart("abc123")
# Expected to leave the leading two spaces and remove the trailing carriage return and new-line characters.
("  Test`r`n").TrimEnd()
# Expected to leave the leading "b" characters and remove the trailing "b" characters.
("bbTestbb").TrimEnd("b")
# Expected to leave the leading "abc123" characters and remove the trailing "abc123" characters.
("abc123Testabc123").TrimEnd("abc123")

[玩转系统] 进行 PowerShell Trim 的绝佳方法(操作指南)

其他 PowerShell 修剪方法

就像 PowerShell 中的所有操作一样,几乎有多种方法可以完成任何操作;修剪琴弦也不例外。您可以使用字符串对象的 SubString()IndexOf() 方法来删除字符串,而不是使用 PowerShell trim() 方法。某个字符串。

请查看下面的 TrimEnd() 与使用 SubString()IndexOf() 方法之间的比较。

# Expected to remove "TeTest" instead of just "Test" at the end of the string.
("StringTeTest").TrimEnd("Test")
# Expected to remove only "Test" at the end of the string.
("StringTeTest").Substring(0,"StringTeTest".IndexOf("Test"))

[玩转系统] 进行 PowerShell Trim 的绝佳方法(操作指南)

SubString 方法有点麻烦。通过从字符串对象内确定测试字符串的索引,并且仅返回从字符串开头到该点的字符,您可以保留剩余的字符串内容,同时仅删除定义的字符串。

结论

PowerShell Trim()TrimStart()TrimEnd() 方法是功能强大的字符串实用程序,用于根据需要清理数据。如果这三种方法不能满足您的需求,PowerShell 提供了几种不同的方法来实现相同的目的,并具有不同的控制级别。立即了解如何应用这些技术在 PowerShell 中修剪字符串!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯