在每次出现 @ 时在 Powershell 中拆分字符串并保存子字符串
Split String in Powershell at every occurence of an @ and save substrings
我有一个字符串可能会根据 gui 中的输入而改变。字符串看起来像这样 "@Donald @Trump @is @orange @and ...) 所以我有 n 个子字符串,我想拆分并像这样保存: $word1 = "Donald" $word2 = "Trump"... $wordn = "xy" 我还需要 n.
的值
PowerShell -split
运算符将执行您想要的操作,除了每个子字符串不是在单独的变量中,而是在数组中。使用您的示例,
$words = "@Donald @Trump @is @orange" -split "@"
会给你一个数组,其中 $words.length
将是 5,单词将在 $words[1]
到 $words[4]
之间($words[0]
是空字符串) .
SS64 has good information on -split
; so does Microsoft Docs.
我有一个字符串可能会根据 gui 中的输入而改变。字符串看起来像这样 "@Donald @Trump @is @orange @and ...) 所以我有 n 个子字符串,我想拆分并像这样保存: $word1 = "Donald" $word2 = "Trump"... $wordn = "xy" 我还需要 n.
的值PowerShell -split
运算符将执行您想要的操作,除了每个子字符串不是在单独的变量中,而是在数组中。使用您的示例,
$words = "@Donald @Trump @is @orange" -split "@"
会给你一个数组,其中 $words.length
将是 5,单词将在 $words[1]
到 $words[4]
之间($words[0]
是空字符串) .
SS64 has good information on -split
; so does Microsoft Docs.