I have an ASP.NET page that has a value stored in the session object.
I want to pull a value out for a specific key — but remember it’s just a string.
Take for example the string below — what’s the best way to get “FOOBAR” from this string? Is regex the best way?
sometimes it could appear this way:
"?facets=All Groups||Brand&TitleTag=FOOBAR#back"
other times it could appear this way:
"?facets=All Groups||Brand&TitleTag=FOOBAR"
UPDATE: SOLUTION
Thanks jglouie for the idea to use ‘ParseQueryString’ — that ultimately did the trick. The code sample you provided was a good start. Below is my final solution:
System.Collections.Specialized.NameValueCollection pairs = HttpUtility.ParseQueryString(facetStateOrURL);
string titleTagValue = pairs["TitleTag"];
if (!String.IsNullOrEmpty(titleTagValue) && titleTagValue.IndexOf('#') > -1)
{
titleTagValue = titleTagValue.Substring(0, titleTagValue.IndexOf('#'));
}
Thanks again for the help!
What about HttpUtility’s ParseQueryString() method?
http://msdn.microsoft.com/en-us/library/ms150046.aspx
—
Added sample with Jethro’s suggestion: