35

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"
4

3 回答 3

58

As of PowerShell 2, simple:

$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. See about_Split for more details.

于 2013-06-10T16:28:58.897 回答
12
[string[]]$recipients = $address.Split('; ',[System.StringSplitOptions]::RemoveEmptyEntries)
于 2013-06-10T16:24:35.770 回答
9

从原始字符串中删除空格并以分号分隔

$address = "foo@bar.com; boo@bar.com; zoo@bar.com"
$addresses = $address.replace(' ','').split(';')

或全部在一行中:

$addresses = "foo@bar.com; boo@bar.com; zoo@bar.com".replace(' ','').split(';')

$addresses变成:

@('foo@bar.com','boo@bar.com','zoo@bar.com')
于 2013-06-10T16:26:23.430 回答