Substring() fails - need to cast result as string instead of default double
在尝试之前,我认为子字符串很容易:
1 2 3 4 | # extract from a webpage with regex into $mmdd the value like 08/09 Write-Host $mmdd 08/09 $mmdd.Substring(0,2) |
然后我得到:
1 2 3 4 5 6 | Method invocation failed because [System.Double] does not contain a method named 'Substring'. At line:1 char:1 + $test.Substring(0,2) + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound |
1 | > $test.Substring(3) |
1 2 3 4 5 6 | Method invocation failed because [System.Double] does not contain a method named 'Substring'. At line:1 char:1 + $test.Substring(3) + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound |
如何将变量
我怀疑我必须对以下内容做一些事情:
1 2 3 4 5 6 |
因为$ mmdd给了我:
1 | 0.888888888888889 |
这是双重信号,无法像下面那样进行投射:
1 | [string]$mmdd.Substring(0,2) |
它将给我
0。
代替
08
只需在
1 | $mmdd.ToString().SubString(0,2) |
但是,也许您告诉我们您的实际问题,因为这可能是XY问题。