这可能有效:
($dirSourceFolder -split "\\")[1]
,
您还可以使用Regex:
$substring = if ($dirSourceFolder -match '\\?(BBG)\\?') { $matches[1] }
或(区分大小写)
$substring = [regex]::Match($dirSourceFolder,'\\?(BBG)\\?').Groups[1].Value
或与上述相同,但现在不区分大小写
$caseInsensitive = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
$substring = [regex]::Match($dirSourceFolder,'\\?(bbg)\\?',$caseInsensitive).Groups[1].Value
全部将输出
BBG
P.S。使用IndexOf
也可以,但是需要更多的努力:
$index = $dirSourceFolder.IndexOf('\BBG\') #'# include the backslashes
if ($index -ge 0) {
$substring = $dirSourceFolder.Substring($index + 1,'BBG'.Length)
}
本文链接:https://www.f2er.com/3160757.html