简体   繁体   English

PowerShell无法删除text和String之间的空格

[英]PowerShell unable to remove space between text and String

In PowerShell I am trying to find a way to remove the text from an output of text and a String. 在PowerShell中,我试图找到一种从文本输出和字符串中删除文本的方法。

Write-Host 'File located at C:\'$Fileline.FilePath -

I get an output of 我得到了一个输出

c:\ program files\path

The space between c:\\ and "Program files" is what I want to remove. c:\\和“程序文件”之间的空格是我想要删除的。 Do I have to convert the text to a string, and then output it as two strings and then remove the spaces? 我是否必须将文本转换为字符串,然后将其输出为两个字符串然后删除空格?

This is happening because you are passing multiple strings to Write-Host , which it is then joining with spaces. 发生这种情况是因为您将多个字符串传递给Write-Host ,然后它将与空格连接。 This behaviour is somewhat unique to Write-Host . 这种行为在Write-Host有些独特。

You can meet your need by sending a single double quoted string to Write-Host , which you can then put your variable inside and it will be expanded. 您可以通过向Write-Host发送一个双引号字符串来满足您的需求,然后您可以将变量放入其中并进行扩展。 However because you are accessing a property of your variable, you need to wrap it in a sub-expression: $() : 但是因为您正在访问变量的属性,所以需要将其包装在子表达式中: $()

Write-Host "file located at C:\$($Fileline.FilePath) -"

Try using the PowerShell -f formatting operator: 尝试使用PowerShell -f格式化运算符:

Write-Host ("File located at C:\{0} -" -f $FileLine.FilePath)

There's good info on -f at SS64 and at TechNet SS64TechNet上有关于-f的良好信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM