Determine the index of a numeric character

1

How can I store the position of the first numeric character of a string ?

Example:

$NomeEstacao = "Atendimento05" # posição 11 (carácter 0)
$NomeEstacao = "Direcao46" # posição 7 (carácter 4)

I tried to use $a.IndexOf("\d"). to find the position but to no avail.

    
asked by anonymous 06.11.2015 / 12:49

1 answer

0

You can do the following:

$NomeEstacao = "Atendimento05" 
if ($NomeEstacao  -match "(?<digito>\d)") {
    $pos = $NomeEstacao.IndexOf($Matches.digito)
    Write-Host $pos
}

The -match looks for the first digit in the string and then the .IndexOf(...) returns the digit position. You still have the bonus if you want to know the first digit found.

    
06.11.2015 / 13:29