Question Découper une chaine de caractère (Résolu)

Plus d'informations
il y a 16 ans 8 mois #4860 par Sébastien MORTREUX
Bonjour à tous !

Pour les besoins d'un script, je dois découper une chaine de caractère. Cependant, ce n'est pas selon un caractères, mais par groupe de caractère. Je m'explique, je récupère dans un fichier csv une date au format 20090624 (24 juin 2009), je souhaite récupérer en 3 variables l'année, le mois et le jour.

Tout ce que j'ai trouvé sur la toile, c'est un découpage selon un caractère, mais rien en fonction du nombre de caractère... Si quelqu'un à une piste :)

Merci d'avance !

OhmWorK<br><br>Message édité par: Arnaud, à: 25/06/09 23:25

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 16 ans 8 mois #4861 par Arnaud Petitjean
Bonjour Ohmwork et bienvenue !

C'est pas très compliqué. Il va falloir jouer un peu avec les chaines de caractères.

Il existe une méthode nommée SubString qui va te permettre de découper ta chaine en petits morceaux.

Pour connaitre la liste des méthodes applicables sur un objet de type Chaine (String) il faut utiliser la commande Get-Member.

Exemple :

[code:1]
$maDate = \&quot;20090624\&quot;
$maDate | Get-Member[/code:1]

ou encore

[code:1]
[String]\&quot;toto\&quot; | GM

TypeName: System.String

Name MemberType Definition
----

Clone Method System.Object Clone()
CompareTo Method System.Int32 CompareTo(Object value), System.Int32 CompareTo(String strB)
Contains Method System.Boolean Contains(String value)
CopyTo Method System.Void CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIn...
EndsWith Method System.Boolean EndsWith(String value), System.Boolean EndsWith(String value, ...
Equals Method System.Boolean Equals(Object obj), System.Boolean Equals(String value), Syste...
GetEnumerator Method System.CharEnumerator GetEnumerator()
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
get_Chars Method System.Char get_Chars(Int32 index)
get_Length Method System.Int32 get_Length()
IndexOf Method System.Int32 IndexOf(Char value), System.Int32 IndexOf(Char value, Int32 star...
IndexOfAny Method System.Int32 IndexOfAny(Char[] anyOf), System.Int32 IndexOfAny(Char[] anyOf, ...
Insert Method System.String Insert(Int32 startIndex, String value)
IsNormalized Method System.Boolean IsNormalized(), System.Boolean IsNormalized(NormalizationForm ...
LastIndexOf Method System.Int32 LastIndexOf(Char value), System.Int32 LastIndexOf(Char value, In...
LastIndexOfAny Method System.Int32 LastIndexOfAny(Char[] anyOf), System.Int32 LastIndexOfAny(Char[]...
Normalize Method System.String Normalize(), System.String Normalize(NormalizationForm normaliz...
PadLeft Method System.String PadLeft(Int32 totalWidth), System.String PadLeft(Int32 totalWid...
PadRight Method System.String PadRight(Int32 totalWidth), System.String PadRight(Int32 totalW...
Remove Method System.String Remove(Int32 startIndex, Int32 count), System.String Remove(Int...
Replace Method System.String Replace(Char oldChar, Char newChar), System.String Replace(Stri...
Split Method System.String[] Split(Params Char[] separator), System.String[] Split(Char[] ...
StartsWith Method System.Boolean StartsWith(String value), System.Boolean StartsWith(String val...
Substring Method System.String Substring(Int32 startIndex), System.String Substring(Int32 star...
ToCharArray Method System.Char[] ToCharArray(), System.Char[] ToCharArray(Int32 startIndex, Int3...
ToLower Method System.String ToLower(), System.String ToLower(CultureInfo culture)
ToLowerInvariant Method System.String ToLowerInvariant()
ToString Method System.String ToString(), System.String ToString(IFormatProvider provider)
ToUpper Method System.String ToUpper(), System.String ToUpper(CultureInfo culture)
ToUpperInvariant Method System.String ToUpperInvariant()
Trim Method System.String Trim(Params Char[] trimChars), System.String Trim()
TrimEnd Method System.String TrimEnd(Params Char[] trimChars)
TrimStart Method System.String TrimStart(Params Char[] trimChars)
Chars ParameterizedProperty System.Char Chars(Int32 index) {get;}
Length Property System.Int32 Length {get;}

[/code:1]



Donc pour utiliser la méthode SubString, tu indiques dans le premier argument la position à partir de laquelle commencer, puis en second la longueur.

Donc pour ton besoin ce sera :

[code:1]
$maDate = \&quot;20090624\&quot;
$annee = $maDate.Substring(0,4)
$mois = $maDate.Substring(4,2)
$jour = $maDate.Substring(6,2)[/code:1]

@+ ;)
Arnaud<br><br>Message édité par: Arnaud, à: 25/06/09 23:26

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 16 ans 8 mois #4864 par Sébastien MORTREUX
Re - Bonjour,

Un très grand merci pour ton explication clair et précise, c'est exactement ce que je cherchais à faire.

OhmWorK

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.054 secondes
Propulsé par Kunena