Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I once came across a function in VB6 that received 3 parameters "Parameter","Match" and "Delimiter" that could extract information from a given string as follows:
SiteAddress=XXXX;UID=XXXX;PWD=XXXX

In which SiteAddress, for example, is a parameter, ";" is the delimiter and "=" is the match. I wonder if anyone can help me have one written in C#.
Posted
Updated 27-Sep-14 4:13am
v2
Comments
[no name] 27-Sep-14 9:48am    
String.Split
Richard MacCutchan 27-Sep-14 9:59am    
That's a solution, and a good one.
Sergey Alexandrovich Kryukov 27-Sep-14 10:11am    
Of course. Regex solution will be much uglier.
—SA
Sergey Alexandrovich Kryukov 27-Sep-14 10:12am    
What have you tried so far?
—SA
PIEBALDconsult 27-Sep-14 10:28am    
Remember that today's passwords should contain "special characters" and so may include your match and delimiter characters so you need to support that.
String.Split won't honor quotes; I'd use a Regular Expression.

1 solution

Why "custom functions"? Try something like
C#
using StringDictionary = System.Collections.Specialized.StringDictionary;

// ...

static StringDictionary ParseKeyValues(string source, char parameterDelimiter, char keyValueDelimiter) {
    StringDictionary result = new StringDictionary();
    string[] parameters =
        source.Split(
            new char[] { parameterDelimiter },
            System.StringSplitOptions.RemoveEmptyEntries);
    foreach (string parameter in parameters) {
        string[] pair = parameter.Split(new char[] { keyValueDelimiter });
        string key = pair[0]; // always exist due to previous split options
        string value = null;
        if (pair.Length > 1)
            value = pair[1];
        result.Add(key, value); // may throw exception
    } //loop
    return result;
} //ParseKeyValues


A Regular Expression solution exist but will be uglier, as the number of parameters is unknown.

—SA
 
Share this answer
 
v2
Comments
BillWoodruff 27-Sep-14 10:52am    
Error in your code: should be: result.Add(key, value); ?

fyi: As of .NET 1.1, in the System.Collections.Specialized Namespace, there is a Dictionary named 'StringDictionary:

http://msdn.microsoft.com/en-us/library/System.Collections.Specialized.StringDictionary(v=vs.85).aspx

It's "hard-coded" to support only Type 'String keys, and values.

I am not competent in "RegEx," but I do wonder, given the "back-tracking" capabilities of Regular Expressions, what you mean by implying it would be difficult to handle an unknown number of parameters: what exactly do you reference when you write "parameters:" do you mean each delimiter parameter could have more than one character in the char[] ?

cheers, Bill
Sergey Alexandrovich Kryukov 28-Sep-14 1:29am    
Sure, this is a bug; thank you very much, fixed.
By a "parameter", I mean each of key/value pairs. It's quite possible to do with Regex, but I see no point. OP shows 3 such parameters, but who knows how many are their. Regex for exactly 3 parameters would be the easiest, but for unknown number of then, one would need to traverse all the recognized groups anyway, which would make the solution longer then this one, and much less readable.
—SA
BillWoodruff 28-Sep-14 2:54am    
Your (corrected) answer gets my 5; it's a good "generalized" lesson on string parsing, and storing the result of parsing in a Dictionary for fast look-up. I don't see why this answer should ever have gotten a #2 vote.

In this particular case, if the user only needs to parse one time for a very limited number of key-value pairs, a Dictionary may be a bit of over-kill. Of course, as usual, we don't know ... and will probably never know ... what the user needs :)

The good thing about examples like this is: if the user understands the code, they can then write other "tailored" versions, possibly more simple, for a wide variety of cases.

The fans of RegEx (I'm not one in most cases) would, I think, argue about the performance of using 'grouping to parse a variable length input into a collection of "chunks" vs. the "hand-coded" approach, particularly if the RegEx is compiled, but such a comparison is beyond my abilities.
Sergey Alexandrovich Kryukov 28-Sep-14 3:00am    
Thank you, Bill.
A dictionary is not for parsing, but for user if the getting values by keys with O(1) time complexity. Yes, it could be an overkill in case of small number of items, but still there is nothing wrong in it. Besides, imagine you aim to write the shortest code to illustrate this parsing, without compromising generalization...
—SA
BillWoodruff 28-Sep-14 3:16am    
Not to belabor this discussion, but, the resulting 'Groups 'found by the RegEx can be stored easily in some structure, or parsed further and stored in a Dictionary structure.

My "bias" against RegEx probably comes from my experience at Adobe where I'd run into undocumented RegEx's that were quite complex and essentially functioned as "black-boxes" that people were afraid to tamper with.

And, of course, I am prejudiced against any formalism I have been too lazy to learn well, since that obviates my guilt, and excuses me from hard-work :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900