Finding Uppercase Characters

by May 25, 2015

If you'd like to find uppercase characters, you could use regular expressions. However, you would then provide a list of uppercase characters to check against. A more flexible way is to use the .NET function IsUpper().

Here is a sample: it scans a text character by character, and returns the position of the first uppercase character:

$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)
}

The result would look like this:

 
PS C:\> 

First uppercase character at position 24
here is some text with U<<<ppercase letters 
 

Twitter This Tip! ReTweet this Tip!