PowerShell实现在字符串中查找大写字母
如果你想找到字符串中的大写字符,你可能会使用正则表达式。亦或者使用你的大写字母列表一个个匹配,当然更灵活的是使用.NET中的IsUpper()函数。
小编注:.NET是PowerShell的土壤,尽最大可能挖掘出这些framework框架中的函数,是我们伸手党永恒的追求。
下面的例子,会扫描字符串中的每一个字符,然后返回遇到的第一个大写字母的位置:
$text='hereissometextwithUppercaseletters'
$c=0
$position=foreach($characterin$text.ToCharArray())
{
$c++
if([Char]::IsUpper($character))
{
$c
break
}
}
if($position-eq$null)
{
'Nouppercasecharactersdetected.'
}
else
{
"Firstuppercasecharacteratposition$position"
$text.Substring(0,$position)+"<<<"+$text.Substring($position)
}
输出结果如下:
PSC:\>Firstuppercasecharacteratposition24 hereissometextwithU<<