Reputation: 46651
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.
Upvotes: 2
Views: 993
Reputation: 422242
input.Split('&').Select(x => x.Split('=')).Single(x => x[0].Equals("ConfirmationId"))[1]
Upvotes: 1
Reputation: 2561
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;
}
Upvotes: 0
Reputation: 59705
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>[^&]+)"
Upvotes: 2
Reputation: 63495
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"
Upvotes: 21
Reputation: 14743
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.
Upvotes: 0
Reputation: 60276
string confirmationId = Regex.Match(input, @"(?<=ConfirmationId=)[^&]*").Value;
Upvotes: 1
Reputation: 38356
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;
Upvotes: 1
Reputation: 416131
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={[^&]*}
Upvotes: 1
Reputation: 12509
You can do the following:
myString.Split('&')[3].Split('=')[1];
But a better way would probably be to use RegEx:
new RegEx("ConfirmationId=?*")
Upvotes: 0
Reputation: 191048
You can do a split on that result and grab the confirmation number.
Upvotes: 0
Reputation: 564851
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];
Upvotes: 3