Skip to content

字符串(Strings)

  • 引号:
    • 单引号 '...':字面量,不解析变量与转义
    • 双引号 "...":内插字符串,解析变量与 ` 转义
  • 拼接:使用 + 或内插
  • 多行字符串(Here-String):@"..."@ 或 @'...'@

示例:

powershell
$name = 'Alice'
"Hello, $name"
'Path: C:\\Temp'  # 单引号无需转义反斜杠
@"
Line1
Line2 $name  # 支持内插
"@

格式化:

powershell
'{0} scored {1:P2}' -f 'Alice', 0.8765

正则:-match 与 $Matches;-replace 支持正则替换。

powershell
'abc123' -match '^(?<prefix>[a-z]+)(?<num>\d+)$' | Out-Null
$Matches.prefix  # abc
$Matches.num     # 123
'foo bar' -replace '\s', '_'  # foo_bar

本站内容仅供学习和研究使用。