0

I have a list of browser agent strings with each string looking like -

Mozilla/5.0 (iPad; CPU OS 6_1 like Mac OS X)
AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0
Mobile/10B141
Safari/8536.25

Using a foreach loop I will be going through a list of these strings. On each iteration I want to extract just the OS version out which I'm assigning to a variable before I proceed to process further.

foreach (var e in AgentStrings)
{
   var myOS = e.UserAgent GET JUST THE OS  (6_1_3) ??

   // will do more stuff here
}

What's the easiest way to essentially retrieve the value between OS and Like in the agent string?

ElectricRouge
  • 1,239
  • 3
  • 22
  • 33
DysonGuy
  • 163
  • 1
  • 11
  • If the list is known I suggest you provide two or three more examples so that the answer will be as accurate as possible. Thanks. – Crono Mar 21 '14 at 17:59

2 Answers2

0

Probably the best way is to check to see if the string contains a short list of OS's you're looking for:

string theOS = "";
var agentString = e.UserAgent;
if(agentString.Contains("Mac OS X")
    theOS = "Mac OS X";
else if(agentString.Contains("Windows 8")
    theOS = "Windows 8";

etc.

That's how MSDN and other Stack Overflow questions seem to recommend doing it

Community
  • 1
  • 1
DLeh
  • 23,806
  • 16
  • 84
  • 128
  • Thanks. I thought about doing something like this. The list of agent strings I'm looping through are only iPad based strings. The only difference is the OS. Unfortunately, I'm seeing many different versions 5_0, 5_1_1, 6_1_2, 6_1_3, etc, etc. I was hoping to avoid creating a large if , else if process to process all the different possible OS version. – DysonGuy Mar 21 '14 at 18:21
0

You could try a splitting the string by a space.

Boolean next = False;
String Version = "";
 foreach (String x in a.split(" ")
  {
      if (next) {Version = x; break;}
      if (x.equals("OS"))
       { next = True;}

  }
Gravy
  • 168
  • 1
  • 10