带有示例的PowerShell拆分字符串操作

Powershell provides string type methods to the string objects. Split is one of them. Split method is used to split string into separate parts or a string array. In this tutorial we will look different usage types and examples of Split.

Powershell为字符串对象提供了字符串类型方法。 Split就是其中之一。 拆分方法用于将字符串拆分为单独的部分或字符串数​​组。 在本教程中,我们将研究不同的用法类型和Split的示例。

指定分隔符 (Specify Separator)

While splitting a text or string we can provide a separator to split them. We will provide as argument to the split function. Function names are caseinsensitive. We will provide space as separator in this example.

在拆分文本或字符串时,我们可以提供一个分隔符来拆分它们。 我们将为split函数提供参数。 函数名称不区分大小写。 在此示例中,我们将提供空间作为分隔符。

$ "my test system is".split(" ")
Specify Separator
Specify Separator
指定分隔符

Each separated element is printed in a new line.

每个分隔的元素都以新行打印。

使用多个分隔符拆分 (Split With Multiple Separators)

In some situations there may be more than one separator. Another useful feature of split function is we can specify multiple separators by adding more split functions and related separator in a string mode. In this example we will define two separator like space and point.

在某些情况下,可能会有多个分隔符。 拆分功能的另一个有用功能是,我们可以通过在字符串模式下添加更多拆分功能和相关的分隔符来指定多个分隔符。 在此示例中,我们将定义两个分隔符,例如space和point。

First we create our separator variable.

首先,我们创建分隔符变量。

$separators=(" ",".")

Then we provide $separators variable to the split function.

然后,我们将$ separators变量提供给split函数。

"my te.st sys.tem is".split($separators)
Split With Multiple Separators
Split With Multiple Separators
使用多个分隔符拆分

根据正则表达式拆分(Split According To Regex)

For more structured but complex strings and text regex based separator can be used. In this example we will use t and s characters as separator by specifying in regex.

对于更结构化但复杂的字符串和基于文本正则表达式的分隔符,可以使用。 在此示例中,我们将通过在正则表达式中指定使用ts字符作为分隔符。

 "my te.st sys.tem is".Split("[ts]")
Split According To Regex
Split According To Regex
根据正则表达式拆分

分割Mac地址(Split Mac Address)

Another useful example will be splitting the mac address into hexadecimal parts.

另一个有用的示例是将mac地址拆分为十六进制部分。

"12-34-56-78-9A-BC".Split("-")
Split Mac Address
Split Mac Address
分割Mac地址

We get all parts of the mac address in a new line. If we need we can loop in them using for each.

我们在新行中获取mac地址的所有部分。 如果需要的话,可以将它们循环使用。

LEARN MORE  How To Get Query String From Url With Javascript?
了解更多信息如何使用Javascript从网址获取查询字符串?

翻译自: https://www.poftut.com/powershell-split-string-operation-examples/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值