Removing more than one white space in powershell

為{幸葍}努か 提交于 2019-11-29 16:57:40

问题


I was trying to find a way in powershell to remove more than one white space.

But what i found is how to do it in php. "Removing more than one white-space"

There will be similar regular expression may available .

How to acheive the same in powershell?

My string is like this

Xcopy Source  Desination

Some lines may contain more than one white space between Source and destination.


回答1:


If you're looking to collapse multiple consecutive whitespace characters into a single space then you can do this using the -replace operator:

'[     Hello,     World!     ]' -replace '\s+', ' '

...returns...

[ Hello, World! ]

The first parameter to -replace is a regular expression pattern to match, and the second parameter is the text that will replace any matches. \s will match a whitespace character, and + indicates to match one or more occurrences, so, in other words, one or more adjacent whitespace characters will be replaced with a single space. Enter help about_comparison_operators or see here for more information.



来源:https://stackoverflow.com/questions/8686816/removing-more-than-one-white-space-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!