By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,644 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,644 IT Pros & Developers. It's quick & easy.

Parse string and grab only certain data

P: 16
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?

Expand|Select|Wrap|Line Numbers
  1. public static string ParseMac(string macOut)
  2.         {
  3.  
  4.  
  5.             for (int i = 0; i < 10; i++)
  6.             {
  7.                 if (i == 4)
  8.                 {
  9.                     char[] delimiterChars = { ' ' };
  10.  
  11.                     string[] outPut = macOut.Split(delimiterChars);
  12.  
  13.                     foreach (string s in outPut) { }
  14.  
  15.                 }
  16.               }
  17.             return (s);
  18.  
  19.             }
Feb 5 '08 #1
Share this Question
Share on Google+
4 Replies


Plater
Expert 5K+
P: 7,872
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
Expand|Select|Wrap|Line Numbers
  1. public static string ParseMac(string macOut)
  2. {
  3.    char[] delimiterChars = { ' ' };
  4.    string[] outPut = macOut.Split(delimiterChars);
  5.    //index is 0,1,2,3,etc so the "fourth" part is [3]
  6.    string PartWanted = outPut[3];
  7. }
  8.  
Feb 5 '08 #2

P: 16
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?
Feb 5 '08 #3

Plater
Expert 5K+
P: 7,872
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.
Feb 5 '08 #4

P: 16
I did this instead:
Expand|Select|Wrap|Line Numbers
  1. string macOut = "Internet      204.189.124.205   16  0000.0c07.ac01   ARPA Vlan101";
  2.  
  3.             Regex r = new Regex(" +");
  4.             string [] splitString = r.Split(macOut);
  5.  
  6.             string PartWanted = splitString[3];
Thanks for the help.
Feb 5 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.