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

 更新时间:2015年06月02日 09:36:29   投稿:junjie  
这篇文章主要介绍了PowerShell实现在字符串中查找大写字母,本文讲解使用.NET中的IsUpper()函数来实现查找大写字母的需求,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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

相关文章

最新评论

?


http://www.vxiaotou.com