views:

685

answers:

15

I have the following string:

FirstName=John&LastName=Doe&City=London&ConfirmationId=WXASL320330

I want to extract the confirmationId from it. I have done a split on it and passed in the & character, but this returns ConfirmationId=WXASL320330 and I just WXASL320330. What is the best way of doing this?

The string is actually part of a property called RawProcessorResult that PayPal returns.

+7  A: 

Another split on '='

chills42
+7  A: 

Use a regular expression:

new Regex( @"ConfimationId=(\w+)" )

tanascius
+3  A: 

You can use Regular Expressions to get there.

Alternatively, you could just split your current result on =, and take the second argument.

string temp = "ConfirmationId=WXASL320330";
string[] portions = temp.Split("=");
string answer = portions[1];
Reed Copsey
A: 

You can do a split on that result and grab the confirmation number.

Daniel A. White
+1  A: 
input.Split('&').Select(x => x.Split('=')).Single(x => x[0].Equals("ConfirmationId"))[1]
Mehrdad Afshari
Even better readability with x[0].Equals("ConfirmationId")
çağdaş
lol :) done!
Mehrdad Afshari
+2  A: 

I would use a regex. ex:ConfirmationId=(\w{11})

Wyvern
A: 

You can do the following:

myString.Split('&')[3].Split('=')[1];

But a better way would probably be to use RegEx:

new RegEx("ConfirmationId=?*")
Max Schmeling
+1  A: 

It depends. If that's part of your url in an asp.net app then you just want Request["Confirmationid"]. If it's from something else then use a regular expresion:

ConfirmationId={[^&]*}

Joel Coehoorn
+1  A: 

You have three main ways of doing it.

var source = "FirstName=John&LastName=Doe&City=London&ConfirmationId=WXASL320330";
var parts = source.Split(new string[] { "&" }, StringSplitOptions.None);
Split again
var ids = parts[3].Split(new string[] { "=" }, StringSplitOptions.None);
var confirmationId = ids[1];
Substring with IndexOf
var confirmationId = parts[3].Substring(parts[3].IndexOf("="));
Regex
var pattern = new Regex("[^=]+=(?<value>.*)");
var match = pattern.Match(parts[3]);
var confirmationId = match.Groups["value"].Value;
Samuel
+2  A: 

I suggest using a regular expression.

String input =
   "FirstName=John&LastName=Doe&City=London&ConfirmationId=WXASL320330";

String confimationId =
   new Regex(@"ConfirmationId=(?<Id>[A-Z0-9]+)(&|$)").
   Match(input).
   Group("Id").
   Value;

If the confirmation id has more structure - for example allways 5 letters and 6 digits - you should include this information into the expression.

@"ConfirmationId=(?<Id>[A-Z]{5}[0-9]{6})(&|$)"

If the exact structure is not known or may change, just get everything up to the next ampersand.

@"ConfirmationId=(?<Id>[^&]+)"
Daniel Brückner
+1  A: 
string confirmationId = Regex.Match(input, @"(?<=ConfirmationId=)[^&]*").Value;
Lucero
This would be my answer, though some explanation might be helpful in the answer.
Greg Ogle
You may be right. However, if one is too slow (because of giving explanations) here the comment is so far below that it doesn't get read at all. In fact, many of the answers here do not do what they are supposed to do (return only the value, not the text also) or do it very inefficiently (string.Split...).Anyways, this is a regular expression featuring a positive lookbehind, so that it only matches the value even though the name is also checked for a successful match.
Lucero
A: 

Is this a querystring in a URL? Are you using a web project? If so, there are built in methods for retrieving the values. Let me know if this is what you are doing and I'll post code.

Mike C.
+21  A: 

The easiest way is to use the built-in HttpUtility.ParseQueryString() method:

string queryString = "FirstName=John&LastName=Doe&City=London&ConfirmationId=WXASL320330";

string confirmationID = 
  System.Web.HttpUtility.ParseQueryString(queryString)["ConfirmationId"]; 
  // returns "WXASL320330"
John Rasch
+1 Bravo, cleanest answer yet
ryeguy
It's always good to know all classes in the FCL! +1
Daniel Brückner
This really is a hidden gem that people miss.
Joe Doyle
A: 

I think this is part of a Web Application, please try to use internal methods get around this, If some one the location of the Keys and their respective value changes, the you will get in to trouble.

A: 

I think the best way is to do is using the string indexes. Make sure you take into account if for some reason the specific string isn't there:

string val = "FirstName=John&LastName=Doe&City=London&ConfirmationId=WXASL320330";
string result = string.Empty;
if (val.LastIndexOf("ConfirmationId=")!= -1)
{
    string val2 = val.Substring(val.LastIndexOf("ConfirmationId=") + 15);
    result = (val2.IndexOf('&') != -1) ? val2.Substring(0, val2.IndexOf('&')) : val2;
}
greektreat