2

I have a string, with data as shown below. How I read separately each property values.

ie PCIaaS_CardId=value; BillingFirstName=value;

PCIaaS_CardId=value&BillingFirstName=value&BillingLastName=value&BillingCompanyNamevalues=&BillingAddress1=value&BillingAddress2=values&BillingCity=value

  • It's not clear what you are asking. I assume the second line is an example of a string to be parsed? Is this a querystring from a URL, or does it just happen to have same/similar format? – Chris Ballard Mar 06 '14 at 10:30

4 Answers4

10

That looks like a HTTP query string, for which you can use HttpUtility.ParseQueryString.

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
CompuChip
  • 9,143
  • 4
  • 24
  • 48
  • 1
    Thanks CompuChip... :). You give a great thread. I found a similar question in http://stackoverflow.com/questions/11956948/easiest-way-to-parse-querystring-formatted-data – ellickakudy rajeesh Mar 06 '14 at 10:32
1
var propertyParts = yourString.Split('&');
foreach (var propertyStr in propertyParts)
{
    var keyValue = propertyParts.Split('=');
}
Sasha
  • 8,537
  • 4
  • 49
  • 76
0

You can convert string to dictionary:

Dictionary<string, string> values = 
    str.Split('&').Select(s => s.Split('=')).ToDictionary(a => a[0], a => a[1]);

Then getting value by key will look like:

string firstName = values["FirstName"];
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

string[] abc = "value&BillingFirstName=value&BillingLastName=value&BillingCompanyNamevalues=&BillingAddress1=value&BillingAddress2=values&BillingCity=value".Split(new char[] { '&' });

Then you can access it by abc[index]

OR

string[] abc = yourString.Split(new char[] { '&' });

Shah Bdr
  • 15
  • 3