您现在的位置是:网站首页> 编程资料编程资料

PowerShell实现在字符串中查找大写字母_PowerShell_

2023-05-26 443人已围观

简介 PowerShell实现在字符串中查找大写字母_PowerShell_

如果你想找到字符串中的大写字符,你可能会使用正则表达式。亦或者使用你的大写字母列表一个个匹配,当然更灵活的是使用.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<< 

-六神源码网