如果你想找到字符串中的大寫字符,你可能會使用正則表達式。亦或者使用你的大寫字母列表一個個匹配,當然更靈活的是使用.NET中的 IsUpper()函數(shù)。
小編注:.NET是PowerShell的土壤,盡最大可能挖掘出這些framework框架中的函數(shù),是我們伸手黨永恒的追求。
下面的例子,會掃描字符串中的每一個字符,然后返回遇到的第一個大寫字母的位置:
$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<<
新聞熱點
疑難解答
圖片精選