Parse string and grab only certain data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prodian
    New Member
    • Jan 2008
    • 16

    Parse string and grab only certain data

    Im trying to parse a recv from a telnet session then only grab certain data.

    Heres an example of the recv that Im storing into a string:

    Internet 204.189.124.205 0 001a.a01f.4e5a ARPA Vlan122

    The only part I want is the mac address which is in bold.

    Im trying to parse it while running it into a for loop so when it gets to the 4th section it will store that in another string. But its not working, anybody have any ideas?

    Code:
    public static string ParseMac(string macOut)
            {
    
    
                for (int i = 0; i < 10; i++)
                {
                    if (i == 4)
                    {
                        char[] delimiterChars = { ' ' };
    
                        string[] outPut = macOut.Split(delimiterChars);
    
                        foreach (string s in outPut) { }
                       
                    }
                  }
                return (s);
                
                }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You have the right idea, but your loops are in the wrong order.

    If you only care about the fourth part, consider something like this
    Code:
    public static string ParseMac(string macOut)
    {
       char[] delimiterChars = { ' ' };
       string[] outPut = macOut.Split(delimiterChars);
       //index is 0,1,2,3,etc so the "fourth" part is [3]
       string PartWanted = outPut[3];
    }

    Comment

    • Prodian
      New Member
      • Jan 2008
      • 16

      #3
      Thanks for the help, thats working fine when theres one space between each word but on the telnet output it could be 1 space or 5 spaces. Anyway around this?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        The split() has an overload to wipe out blank entries (which would happen if two or more spaces are together), use that and it should be ok.

        Comment

        • Prodian
          New Member
          • Jan 2008
          • 16

          #5
          I did this instead:
          Code:
          string macOut = "Internet      204.189.124.205   16  0000.0c07.ac01   ARPA Vlan101";
          
                      Regex r = new Regex(" +");
                      string [] splitString = r.Split(macOut);
          
                      string PartWanted = splitString[3];
          Thanks for the help.

          Comment

          Working...