Cast all keys in dictionary to uppercase
Asked Answered
C

4

5

This is probably a very simple question but google has let me down sofar and keeps pointing me towards python solutions.

I have a webpage where applciations/users can supply querystringparameters.To Retrieve the querystring parameters I use the following code:

IDictionary<string, string> qStrings = HtmlPage.Document.QueryString;

to check the presence of a specified key, I use the following code:

if (!String.IsNullOrEmpty(qStrings["PARAM1"]))
{}

Knowing our users, i'm expecting them to give parameterkeys as follows: "Param1", "param1", "pArAm1" How can simply cast every key in a dictionary to uppercase without iterating each key-valuepair?

Or how can i alter the qStrings["PARAM1"] so it ignores the case?

Counterreply answered 11/9, 2014 at 6:53 Comment(3)
Don't do !String.IsNullOrEmpty(qStrings["PARAM1"]), that will crash if PARAM1 doesn't exist. What you should do is if(qStrings.TryGetValue("PARAM1", out param1)) { ... }Litigate
@Alxandr, thanks, you just saved me a whole lot of support :)Counterreply
Do note that param1 might be null or empty string still though, so you might want to check that inside the { ... }Litigate
S
9

You can use StringComparer to find keys ignoring their case:

var qStrings = new Dictionary<string, string>(
    HtmlPage.Document.QueryString,
    StringComparer.OrdinalIgnoreCase)
Smirk answered 11/9, 2014 at 6:57 Comment(2)
Heads up that this will fail if you have both param1 and ParAM1 in the query string. You might want to guard against that somehow.Litigate
@Alxandr, Again you make a very good point. i've added a check for that to my code-behind!Counterreply
S
2

Simplest Way

qStrings = qStrings .ToDictionary(k => k.Key.ToUpper(), k => k.Value.ToUpper());
Siward answered 26/2, 2020 at 14:16 Comment(0)
F
1

Maybe you can do it like below:

Dictionary<string, string> qStrings = new Dictionary<string, string>();
foreach (var a in qStrings.Keys)
{
    switch (a.ToUpper())
    {
        case "PARAM1":
            break;
    }
}
Fluky answered 11/9, 2014 at 7:13 Comment(1)
Your solution could/should work (haven't tested it). But takemyoxygen solutions lies more in line with the existing code (+1 though)Counterreply
P
-1

Without iterating is not possible. No matter what approach you use there is going to be some sort of iteration. The this is you need to limit the insertion of the data to a single unified casing rather than allowing users to input all sorts of casing.

Taking your example: "Param1", "param1", "pArAm1", a key will be created for each single one of these as they are treated as separate entities. The best way to handle that is to force the casing at the insertion rather than when querying for values.

For example:

void AddToDictionary(string key, string value)
{
   qStrings[key.ToUpper()] = value;
}
Pointer answered 11/9, 2014 at 7:5 Comment(4)
As pointed out in another answer (https://mcmap.net/q/1934417/-cast-all-keys-in-dictionary-to-uppercase), you can ignore case when looking up keys in the dictionary.Kaka
@Kaka but that will only impose a problem as you will have multiple entries with the same key but different casingPointer
@RedSerpent: Just checked it. Even with different casing an exception is thrown when adding twice the same key. so no problems there :)Counterreply
@N55PEC Is right. albeit with one gotcha to be aware of: Assume you init a Dictionary B from the values of another Dictionary A, and B is set to ignore case, but A was not. Then A could contain items with keys that would be duplicates in B because of the differences in casing; this would result in an argument exception when initializing B, due to duplicate keys. This should not be a problem in your case though, unless someone passes two query string parameters that only differ in casing - in which case an exception would be thrown when copying from HtmlPage.Document.QueryString.Kaka

© 2022 - 2024 — McMap. All rights reserved.