简体   繁体   中英

how to name items in a string group?

Say

string[] strGroup=returnStr.split(',');

How to name each item of strGroup , if it has a meaning list of each item? So when I need to use a specific item I just use strGroup["LastName"] instead of strGroup[23] .

Something like this (crazy LINQ talk, I know)

string returnStr = "Claus,Junker,Jørgensen";

string[] keys = new string[]
{
    "FirstName",
    "MiddleName",
    "LastName"
};

Dictionary<string, string> groups = returnStr.Split(',')
         .Select((x, i) => Tuple.Create(keys[i], x))
         .ToDictionary(k => k.Item1, v => v.Item2);

// Alternative version

Dictionary<string, string> groups = returnStr.Split(',')
         .Zip(keys, (k, v) => new KeyValuePair<string, string>(k, v))
         .ToDictionary(k => k.Key, v => v.Value);

An array does not have a string indexer , so this is not possible as such.

You will need to define a name for each part of your string - Split will not do that for you.

You could use a structure that has a defined string indexer , but you will still need to define the key for each item.

There is no magical way that this definition will occur, unless you do have the meta data for each field, somewhere. For example, some CSV parsers will produce such a collection by using the first row as field names.

I suspect you are trying to use split to read a CSV file/stream.

What you can do is store the first line in an array, using split and then, while iterating the lines withing the file, you can reference like this:

string[] strGroup=returnStr.split(',');
var someThing = strGroup[GetIndex("LastName")];

and define GetIndex like this:

public int GetIndex(string str)
{
    return Array.IndexOf<string>(headerArray, str);
}

I see alot of Dictionary examples, but if you are embedding knowledge about the row format in your code, why not make a class of it? Use the constructor to turn returnStr into properties:

class Group
{
    public string FirstName { get; private set;}
    public string LastName { get; private set;}
    [...]

    public Group(string returnStr)
    {
        string[] strGroup=returnStr.split(',');
        [...]
        this.FirstName = strGroup[22];
        this.LastName = strGroup[23];
        [...]
    }
 }

With this approach, you won't have to maintain a set of string constants. If that is a win or not depends on your specific problem.

I think you need to be looking at a dictionary of strings.

string[] data = returnStr.Split(',');
var map = new Dictionary<string, string>();
map["LastName"] = data[23];
map["FirstName"] = data[24]; // or whatever

Now use the map rather than the string array.

string firstName = map["FirstName"];

This is what's the Dictionary<TKey,TValue> class is for. In your case you need a Dictionary<string,string> . However you'll have to initialize it once before you can access items via their name.

When you have only one string use the Dictionary<string, string> datatype.

When you have a whole list of splitted strings, use DataTable and use column names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM