powershell - 使用 Powershell 将多行对象转换为多个对象
问题描述
我有一个变量$c
。
$c
返回
apples
oranges
bananas
如何制作这些单独的对象?
$fruit = $C | foreach { $_ -like 'oranges'}
if($fruit)
{
write-host "Success" -ForegroundColor Green
}
else
{
write-host "Failed" -ForegroundColor red
}
除非我使用通配符,否则这不起作用?
我试图得到一个完全匹配的检查每一行。
任何帮助表示赞赏。我坚持这个。
解决方案
使用-split
运算符:
$multiLineString = @'
apples
oranges
bananas
'@
$arrayOfStrings = $multiLineString -split '\r?\n'
\r?\n
要么匹配CR LF
,要么单独匹配,LF
因此它适用于输入字符串中的 UNIX 和 Windows 样式的行分隔符。
推荐阅读
- java - 案例中如何使用涉及实例化的switch语句?
- node.js - 在节点 js 中找不到“nd sampleApp”命令
- javascript - 解析器(js)的正则表达式lookbehind替代
- python - 在 for 循环中生成时识别按钮
- puppet - puppet 资源如何在不编译失败的情况下检查 puppetmaster 上的数据源?
- c# - C#如何从IEnumerable中创建一个列表
- google-chrome-extension - Chrome 扩展错误:拒绝执行内联事件处理程序
- javascript - React-router API - 定义路由
- unity3d - Unity 找不到 C# 脚本
- r - 基于多条件创建一个新变量并在R中循环