华域联盟 PowerShell 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<<

本文由 华域联盟 原创撰写:华域联盟 » PowerShell实现在字符串中查找大写字母

转载请保留出处和原文链接:https://www.cnhackhy.com/15112.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部