0

I have this string that looks like this

string postData = "event_MemberGUID2=&event_EventID2=6888591&event_ItemID=10151249&event_ItemType=&event_ItemStatusDate=7%2F19%2F2017&event_ItemSemTopic=Test Seminar Topic&event_ItemSemType=100&event_ItemRegion=101A&event_ItemSemPointValue=0&event_ItemStartDate=06%2F05%2F2017&event_ItemEndDate=06%2F05%2F2017&event_ItemStatusID=3";

and I am trying to get the event_ItemID

this is how I am able to get it

List<string> lst = new List<string>();
List<string> lstB = new List<string>();

lst = postData.Split('&').ToList();

foreach(var a in lst)
{
    if (a.Contains("ItemID"))
    {
        lstB = a.Split('=').ToList();
        Console.WriteLine(lstB[1]);                    
    }

}

But is there an easier and more elegant way to get the event_ItemID than how I am extracting it?

Chris
  • 2,953
  • 10
  • 48
  • 118
  • What about regex? – Lloyd Jul 19 '17 at 16:16
  • @Lloyd, i am not very familiar with using regex – Chris Jul 19 '17 at 16:17
  • Personally, I don't think there is anything wrong with your code, It works. You could consider revising regex to do a similar job. Also, maybe you would be better moving this post to the [code review section on Stack Exchange](https://codereview.stackexchange.com/). – Joshua Duxbury Jul 19 '17 at 16:21
  • 1
    Regex is the wrong tool for this; it's both too powerful (it includes many other features that are going to get in your way) and too weak (you have to specify patterns, groups, and extract substrings yourself). Use `HttpUtility.ParseQueryString` as in the duplicate. – Dour High Arch Jul 19 '17 at 16:25
  • @DourHighArch, yep, I looked at the solutions in that link and the ParseQueryString works perfectly – Chris Jul 19 '17 at 16:26

0 Answers0