0

I've got a string fdf=232232&lid=19974832&number=1&aa_result1_1=someId1&aa_resuuuuuult2_2=someId2&aa_resuuuult3_3=someId3

and if aa exists I need to take values and add them to dictionary like:

var dict =  extendedIds.Add("result1", new Dictionary<string, int[]>()
   {
      {
           "someId1",
           new int[]{ 1 }
      }, ...
   });

however I am having a difficult time deciding how to parse it properly? I need to accept multiple aa values (the ones that come as resultN, someIdN and a number (which is the number after resultN_NUMBER).

I tried to use substring but that doesn't work as I dont't now the length of word result

Basically it is var parameters = $"pam=805700&laaid=19974832&kpm=1&{HttpUtility.UrlEncode("aa_{result}_{number}={id}&aa_{result}_{number}={id}&aa_{result}_{number}={id}", Encoding.UTF8)}";

So I decode it and get string: var decoded = input.ToString().UrlDecode();

I need to accept multiple aa values, so in this example there would be three values, two of them comes from in bertween _ one after = but I wonder how to take these values then there could be something else also split by _...

also I could var parsed = HttpUtility.ParseQueryString(decoded); parse to NameValueCollection. but I can't use parsed.GetValues("aa") because the key would be e.g. aa_result1_1 and I never know beforehand what it is

6
  • 1
    learn.microsoft.com/en-us/dotnet/api/… Commented Mar 17, 2021 at 7:11
  • It won't be a word result in reality Commented Mar 17, 2021 at 7:14
  • yes! (also not aa in reality) Commented Mar 17, 2021 at 7:17
  • I mean I changed these values now for an example, in real life result will be some kind of url, aa will be different string but it will be same everywhere Commented Mar 17, 2021 at 7:26
  • 2
    Can you explain the rules by which you go from your input string to your dictionary? Would a Split on _ help? Commented Mar 17, 2021 at 7:28

2 Answers 2

4

this is a query string, you can use HttpUtility.ParseQueryString to parse it

see

https://learn.microsoft.com/en-us/dotnet/api/system.web.httputility.parsequerystring?view=net-5.0

Sign up to request clarification or add additional context in comments.

Comments

1

Would this set you on the right track?

var qs = "fdf=232232&lid=19974832&number=1&aa_result1_1=someId1&aa_resuuuuuult2_2=someId2&aa_resuuuult3_3=someId3";
var nvc = System.Web.HttpUtility.ParseQueryString(qs);
foreach (var key in nvc.AllKeys.Where(k => k.StartsWith("aa")))
{
    var id = nvc[key];
    var parts = key.Split('_');
    var result = parts[1];
    var number = parts[2];
    
    Console.WriteLine($"result = '{result}', number = '{number}' => id = '{id}'");
}
  • Use ParseQueryString to convert your string into a NameValueCollection.
  • Then use each key that starts with "aa"
  • Get its value - this is your "id"
  • Split the key on the _
  • Ignore the first part (which would be "aa") and use the next two parts

Of course you would want to add some safety: I now assume that there always are 3 parts in that key. Plus you want to do something useful with the results.

The above code prints this

result = 'result1', number = '1' => id = 'someId1'
result = 'resuuuuuult2', number = '2' => id = 'someId2'
result = 'resuuuult3', number = '3' => id = 'someId3'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.