[英]How to split a string content into an array of strings in PowerShell?
I have a string that has email addresses separated by semi-colon:我有一个字符串,其中的电子邮件地址以分号分隔:
$address = "foo@bar.com; boo@bar.com; zoo@bar.com"
How can I split this into an array of strings that would result as the following?如何将其拆分为如下结果的字符串数组?
[string[]]$recipients = "foo@bar.com", "boo@bar.com", "zoo@bar.com"
As of PowerShell 2, simple:从 PowerShell 2 开始,简单:
$recipients = $addresses -split "; "
Note that the right hand side is actually a case-insensitive regular expression , not a simple match.请注意,右侧实际上是一个不区分大小写的正则表达式,而不是简单的匹配。 Use csplit
to force case-sensitivity.使用csplit
强制区分大小写。 See about_Split for more details.有关更多详细信息,请参阅about_Split 。
[string[]]$recipients = $address.Split('; ',[System.StringSplitOptions]::RemoveEmptyEntries)
Remove the spaces from the original string and split on semicolon从原始字符串中删除空格并用分号分割
$address = "foo@bar.com; boo@bar.com; zoo@bar.com"
$addresses = $address.replace(' ','').split(';')
Or all in one line:或者全部在一行中:
$addresses = "foo@bar.com; boo@bar.com; zoo@bar.com".replace(' ','').split(';')
$addresses
becomes: $addresses
变为:
@('foo@bar.com','boo@bar.com','zoo@bar.com')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.