【发布时间】:2012-03-11 11:16:47
【问题描述】:
看来 PowerShell -split 运算符和 .NET Split() 方法的行为完全不同。
.NET 将分隔符字符串视为字符数组。
$str = "123456789"
Write-Host ".NET Split(): "
$lines = $str.Split("46")
Write-Host "Count: $($lines.Length)"
$lines
$str = "123456789"
Write-Host "-split operator: "
$lines = $str -split "46"
Write-Host "Count: $($lines.Length)"
$lines
输出:
.NET Split():
Count: 3
123
5
789
-split operator:
Count: 1
123456789
有没有办法让 .NET 应用程序使用与 PowerShell 相同的技术,并将字符串分隔符用作一个实体单元?希望没有 RegEx。
这在 PowerShell 中工作,使用 Split():
Write-Host "Divided by 46:"
"123456789".Split([string[]] "46", [StringSplitOptions]::None)
Write-Host "`n`nDivided by 45:"
"123456789".Split([string[]] "45", [StringSplitOptions]::None)
Divided by 46:
123456789
Divided by 45:
123
6789
【问题讨论】:
-
奇怪!好在我不使用 Powershell ;p
标签: .net string text powershell