我正在尝试从一个字符串值中提取两段信息。第一段信息是从倒数第四个字符到倒数第二个字符; 第二段信息是从倒数第二个字符到最后一个字符。这是我正在使用的代码:
foreach ($item in $List)
{
$len = $item.Length
$folder1 = $item.Substring(($len - 2), $len)
$folder2 = $item.Substring(($len - 4), ($len - 2))
..
}
这段代码在使用 Substring 函数时一直报错。错误描述如下:
*Exception calling "Substring" with "2" argument(s): "Index and length must refer to a
location within the string.
Parameter name: length"
At line:7 char:1
+ $str.Substring($flen - 2, $slen)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException*
如何使用Substring?我应该传递哪些参数?