TUTOS.EU

Retourner la position d'un caractère dans une chaine

Trouver la première ou dernière position d'un caractère dans un texte

On a ici une variable qui contient les caractères
123.567.9

On va y rechercher la position du premier et dernier point

$NomVariable = "123.567.9"

#Le premier caractère, ici 1, est à la position 0
#La recherche du premier point va ici retourner 3
$Position = $NomVariable.indexof(".")
write-host "Première position : $Position"

#La recherche du denier point va ici retourner 7
$Position = $NomVariable.lastindexof(".")
write-host "Dernière position : $Position"
Lien vers le fichier : cliquez ici Copier le code

A partir de là on peut isoler ce qui se situe avant ou après un caractère recherché.

Cet exemple va rechercher \ et afficher "Domain" "MonLogin" sur 2 lignes différentes

clear-host

$NomVariable = "Domain\MonLogin"

$Domaine = $NomVariable.substring(0, $NomVariable.indexof("\"))
$Login = $NomVariable.substring($NomVariable.indexof("\")+1)

write-host $Domaine
write-host $Login
Lien vers le fichier : cliquez ici Copier le code

Pages Web

Site WebDescription
Technet.microsoft.comConverting VBScript's InStr Function
Technet.microsoft.comConverting VBScript's InStrRev Function

Article(s) précédent(s)

2