示例字符串:?300 dpi
@{N='Dpi' ; E={$_.'Horizontal resolution'.Split(" ")[0]}}
我使用split来成功删除dpi
,尽管我还想删除字符串开头的?
。
"Name","Path","BaseName","Dpi","Width(Pixels)","Height(Pixels)","DpiTest"
"test.png","\\directory\TCG\Labels\test.png","test","?300","?2623","?1229","?2623 pixels"
答案 0 :(得分:3)
您可以使用TrimStart()
方法删除字符串开头的一个或多个字符:
$_.'Horizontal resolution'.Split(" ")[0].TrimStart('?')
但我建议使用-replace
运算符进行两种操作:
$_.'Horizontal resolution' -replace '\?(\d+).*','$1'
正则表达式匹配文字?,1个或多个数字 d igits和任何内容,然后将其替换为数字
答案 1 :(得分:0)
就这样做:
$_.'Horizontal resolution'.Split(" ")[0].Replace('?', '')