gpt4 book ai didi

powershell - 将冒号分隔的字符串转换为 PowerShell 字典

转载 作者:行者123 更新时间:2023-12-03 23:26:51 24 4
gpt4 key购买 nike

我正在尝试将冒号分隔字符串转换为 PowerShell 字典。以下是字符串。

$inputkeyvalues = "Appsetting:true|environment:prod"

我在 $inputkeyvalues 中有两个键值对变量和那些由管道分隔符分隔。
第一个是: Appsetting:true
第二个是:环境:产品

我正在尝试转换为 PowerShell 字典。最终输出应该是这样的,
Key            Value
----- -----
Appsetting true
environment prod

$Dictionary= New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"

有人可以为此建议我可能的解决方案。提前致谢。

最佳答案

使用 hashtable :

$inputkeyvalues = "Appsetting:true|environment:prod"

# Create hashtable
$Dictionary = @{}
# Split input string into pairs
$inputkeyvalues.Split('|') |ForEach-Object {
# Split each pair into key and value
$key,$value = $_.Split(':')
# Populate $Dictionary
$Dictionary[$key] = $value
}

关于powershell - 将冒号分隔的字符串转换为 PowerShell 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38470602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com