IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

comment prélever des infos précises dans une chaine


Sujet :

C#

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut comment prélever des infos précises dans une chaine
    Bonjour.

    J'aimerais avoir votre aide pour prélever des informations dans une chaine de caractère. L'utilisation des expressions régulières me semble nécessaire ici, mais si je me trompe, dites-le moi .

    voilà comment je cré une commande :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string command = String.Format("Action:Login,Params:[Identifier:{0},Password:{1}]", Txt_Identifier.Text, Txt_Password.Password);
    je l'envoi à mon serveur, lequel la reçoit et doit prélever les informations.

    J'aimerais utiliser un string.format à l'arrivée (ou autre chose) pour décoder ma chaine de la même façon que je l'ai créée.

    Pouvez-vous m'aider svp ?

  2. #2
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2008
    Messages
    231
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2008
    Messages : 231
    Points : 359
    Points
    359
    Par défaut
    Dans un premier temps tu dois créer un parser qui va parser (traduire) ta commande. Tu peux le faire via des expressions régulières, ou via des séquences, si tu es sur que se sera toujours (pour toutes tes commandes) structuré de la même manière.
    Exemple : @TypeDeCommande:NatureCommande,@params[paramName1:paramValue1,...]
    @TypeDeCommande = Action, etc.
    NatureCommande = Login, Administration, etc.
    paramName1 = Identifier
    paramValue1 = Toto
    etc.

    Si tu veux faire plus générique alors utilise des RegEx

    Dans un second temps regarde ce Design Pattern Command

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    j'ai déjà commencé la création d'un parseur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace Games.ClassLibrary.Network
    {
        public class CommandParser
        {
            public string Method { get; set; }
            public Dictionary<string, string[]> Parameters { get; set; }
            public string CommandInLine { get; set; }
     
            public CommandParser(string Method, Dictionary<string, string[]> Parameters)
            {
                this.Method = Method;
                this.Parameters = Parameters;
                this.CommandInLine = this.EncodCommand();
            }
     
            public CommandParser(string CommandInLine)
            {
                this.CommandInLine = CommandInLine;
     
                string Method = string.Empty;
                Dictionary<string, string[]> Parameters = null;
                this.DecodeCommand(out Method, out Parameters);
                this.Method = Method;
                this.Parameters = Parameters;
            }
     
            private string EncodCommand()
            {
                List<string> serializedParameter = new List<string>();
                foreach (KeyValuePair<string, string[]> parameter in this.Parameters)
                {
                    serializedParameter.Add(String.Format("{0}:{1}", parameter.Key, string.Join(",", parameter.Value)));
                }
                return String.Format("Method:{0},Parameters:[{1}]", this.Method, string.Join(",", serializedParameter));
            }
     
            private void DecodeCommand(out string Method, out Dictionary<string, string[]> Parameters)
            {
                Method = string.Empty;
                Parameters = null;
            }
        }
    }
    je viens tout juste de finir le code que tu vois.
    La méthode pour créer une commande en ligne fonctionne. Maintenant il ne me reste plus qu'à décoder une ligne.

    Je vais essayer avec Regex, merci .

  4. #4
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Si tu veux te simplifier la vie, utilise plutôt un format à un seul niveau comme ceci:
    "Action:Login,Params.Identifier:{0},Params.Password:{1}"

    Ensuite, si on a une string InputCmd de la forme "Nom1:Valeur1,Nom2:Valeur2,...".
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    String[] NomsValeurs = InputCmd.Split(',') ;
    String NomValeur ;
    for (int i=0;i<NomsValeurs.Length;i++) if ((NomValeur=NomsValeurs[i].split(':')).Length>=2)
      switch(NomValeur[0])
     {
         case "Nom1" : Value_for_Nom1 = NomValeur[1]) ; ... ; break ;
         case "Nom2" : Value_for_Nom2 = NomValeur[1]) ; ... ; break ;
         ...
     }

  5. #5
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    Merci Graffito, mais a solution ne marchera pas. Je compte faire passer une liste de valeurs pour 1 paramètre, séparés par des virgules.
    D'ailleurs j'ai presque finis.

    voici mon modèle :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    "Method:'{0}',Parameters:[{1}]"
    voilà ce que j'utilise pour l'extraction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Regex regex = null;
     
                regex = new Regex(@"Method:'(\w+)',Parameters:[(.+)]");
                string[] datas = regex.Split(this.CommandInLine);
    Cependant je n'obtiens que la ligne entière à la sortie.

    Qu'est-ce qui cloche ?

  6. #6
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    voilà ma solution :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    using Newtonsoft.Json;
    using System.Collections.Generic;
     
    namespace Games.ClassLibrary.Network
    {
        public class Command
        {
            public string Method { get; set; }
            public Dictionary<string, string[]> Parameters { get; set; }
     
            public Command(string Method, Dictionary<string, string[]> Parameters)
            {
                this.Method = Method;
                this.Parameters = Parameters;
            }
        }
     
        public class CommandParser
        {
            public Command Command { get; set; }
            public string CommandInLine { get; set; }
     
            public CommandParser(string Method, Dictionary<string, string[]> Parameters)
            {
                this.Command = new Command(Method, Parameters);
                this.CommandInLine = JsonConvert.SerializeObject(this.Command);
            }
     
            public CommandParser(string CommandInLine)
            {
                this.Command = JsonConvert.DeserializeObject<Command>(CommandInLine);
                this.CommandInLine = CommandInLine;
            }
        }
    }

  7. #7
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2008
    Messages
    231
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2008
    Messages : 231
    Points : 359
    Points
    359
    Par défaut
    Petite question as tu des problématiques de performance ?
    Car le JsonConvert n'est peut être pas l'outil le plus adapté si tu as des contraintes de performance.
    Je dis ça car je vois que c'est pour un jeu.

    Attention, je ne fais que supposer que JsonConvert doit utiliser de la reflection et la Reflection != Performance

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 28/03/2008, 18h24
  2. [C#] Comment insérer des retours chariot dans une chaine de caractère ?
    Par tazmania dans le forum Accès aux données
    Réponses: 4
    Dernier message: 30/10/2006, 09h27
  3. Réponses: 3
    Dernier message: 24/04/2006, 15h53
  4. Comment stocker des mots clés dans une bas Mysql
    Par renofx1 dans le forum SQL Procédural
    Réponses: 5
    Dernier message: 05/01/2006, 00h57
  5. Réponses: 2
    Dernier message: 12/01/2004, 13h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo