简体   繁体   English

如何在2D词典中拆分字符串

[英]How to split a string of characters in a 2D dictionary

So i have to put and split this information that is stored in the string Content: 因此,我必须放入并拆分存储在字符串Content中的此信息:

ID_PLAYER_1==>Joueur 1---Player 1
ID_AMMO==>Muni---Ammo

in a 2-d dictionnary that looks like this: 在二维字典中如下所示:

Dictionary<Language, Dictionary<string, string>> items = 
    new Dictionary<Language, Dictionary<string, string>>();

the enumeration Language will determinate the position of one the characters based on the language of the word. 枚举语言将根据单词的语言确定一个字符的位置。

So in the end it would look like this: 所以最后看起来像这样:

Player 1  Ammo
Joueur 1  Muni

I have never used a 2-d dictionary before and i am confused on how i am supposed to split it like that. 我以前从未使用过2-D字典,我对应该如何将其拆分感到困惑。 the method i am working on to do the tranformation of the text like is like this: 我正在做的文本转换方法是这样的:

public ErrorCode Parse()
{    
    string Content = "@ID_PLAYER_1==>Joueur 1---Player 1
    ID_AMMO==>Muni---Ammo"
    //Content has the value: " ID_PLAYER_1==>Joueur 1---Player 1
    //ID_AMMO==>Muni---Ammo"
    items[0].Add (Content.Split(//splitting method), Content.Split())
    return ErrorCode.MISSING_FIELD;
}

Enumeration Language: 枚举语言:

public enum Language
{
  French,
  English
}

ErrorCode: 错误代码:

public enum ErrorCode
{
 OK,
 BAD_FILE_FORMAT,
 MISSING_FIELD,
}

You can use String.Split() method that takes strings as the separator. 您可以使用将字符串作为分隔符的String.Split()方法。

So first you can split the ID from the content. 因此,首先您可以从内容中拆分ID。 And then split the content in French / English. 然后将内容拆分为法语/英语。

With all the values put them where you want, ex: 将所有值放在您想要的位置,例如:

var items = new Dictionary<Language, Dictionary<string, string>>();
items.Add(Language.English, new Dictionary<string, string>() );
items.Add(Language.French, new Dictionary<string, string>() );

var input = "ID_PLAYER_1==>Joueur 1---Player 1";

// parts[0] = ID ; parts[1] = Content
var parts = input.Split(new string[] { "==>" }, StringSplitOptions.None);

// values[0] = french ; values[1] = english
var values = parts[1].Split(new string[] { "---" }, StringSplitOptions.None);

var key = parts[0]; // ID
var french = values[0];
var english = values[1];

// put them in the Dictionary
items[Language.English].Add(key, english);
items[Language.French].Add(key, french);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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