Removing more than one white space in powershell

血红的双手。 提交于 2019-11-30 11:37:43

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.

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