Xaisoft
Xaisoft

Reputation: 46651

How do I extract a string out of a string in c#

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

Answers (14)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422242

input.Split('&').Select(x => x.Split('=')).Single(x => x[0].Equals("ConfirmationId"))[1]

Upvotes: 1

greektreat
greektreat

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

Daniel Brückner
Daniel Brückner

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

John Rasch
John Rasch

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

Mike Cole
Mike Cole

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

Lucero
Lucero

Reputation: 60276

string confirmationId = Regex.Match(input, @"(?<=ConfirmationId=)[^&]*").Value;

Upvotes: 1

Samuel
Samuel

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

Joel Coehoorn
Joel Coehoorn

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

Max Schmeling
Max Schmeling

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

Wyvern
Wyvern

Reputation: 51

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

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 191048

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

Upvotes: 0

Reed Copsey
Reed Copsey

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

tanascius
tanascius

Reputation: 53964

Use a regular expression:

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

Upvotes: 7

chills42
chills42

Reputation: 14533

Another split on '='

Upvotes: 7

Related Questions