如果你想找到字符串中的大写字符,你可能会使用正则表达式。亦或者使用你的大写字母列表一个个匹配,当然更灵活的是使用.net中的 isupper()函数。
小编注:.net是powershell的土壤,尽最大可能挖掘出这些framework框架中的函数,是我们伸手党永恒的追求。
下面的例子,会扫描字符串中的每一个字符,然后返回遇到的第一个大写字母的位置:
$text = 'here is some text with uppercase letters'
$c = 0
$position = foreach ($character in $text.tochararray())
{
$c++
if ([char]::isupper($character))
{
$c
break
}
}
if ($position -eq $null)
{
'no uppercase characters detected.'
}
else
{
"first uppercase character at position $position"
$text.substring(0, $position) + "<<<" + $text.substring($position)
}
输出结果如下:
ps c:\>first uppercase character at position 24 here is some text with u<<
发表评论