如何在c#中动态地映射字符串?

时间:2013-08-30 03:08:13

标签: c# string dictionary

string originaltext = "A man meet a man";
string spintext ="A {man|Person} meet a {man|male}";

每个原始文本都可以有一些旋转选项,因此每个单词都是指向旋转选项词的开始索引...

例如: 首先出现的“男人”有spinoption“{man | Person}”.. 第二次出现的“男人”有旋转选择“{man | male}”......

如果原始文字发生变化,则所有单词索引都在变化...... 所以我想指出新的索引...

 public Dictionary<int, SpintaxMappedValue> SpintaxListDict
 {
     get
     {
         return _spintaxListDict;
     }
     set
     {
         _spintaxListDict = value;
     }
 }

 internal static void AlterSpintaxList(string _NeworiginalText)
 {
     //Build new dictionary for current text
     var _NeworiginalTextDict = Regex.Matches(_NeworiginalText, @"\b\w+\b").Cast<Match>().ToDictionary(m => m.Index, m => m.Value);

     //Comparing old original value dict with new original value dict
     bool dictionariesEqual =
         _NeworiginalTextDict.Keys.Count == Init.SpintaxEditorPropertyMain.OriginalTextDict.Keys.Count &&
         _NeworiginalTextDict.Keys.All(k => Init.SpintaxEditorPropertyMain.OriginalTextDict.ContainsKey(k) && 
         object.Equals(Init.SpintaxEditorPropertyMain.OriginalTextDict[k], _NeworiginalTextDict[k]));

     //do mapping if dictionaries are not Equal
     if (dictionariesEqual == false)
     {
         Dictionary<int, SpintaxMappedValue> TempSpintaxDict = new Dictionary<int, SpintaxMappedValue>(Init.SpintaxEditorPropertyMain.SpintaxListDict);
         Dictionary<int, SpintaxMappedValue> NewSpintaxListDict = new Dictionary<int, SpintaxMappedValue>();

         foreach (KeyValuePair<int, SpintaxMappedValue> olditem in TempSpintaxDict)
         {
             foreach (KeyValuePair<int, string> newitem in _NeworiginalTextDict)
             {
                 if (olditem.Key != newitem.Key)
                 {
                     if (olditem.Value.OriginalWord == newitem.Value)
                     {
                         //if (newitem.Key > olditem.Key)
                         //{
                         olditem.Value.OriginalWordStartingPosition = newitem.Key;
                         olditem.Value.SpinWordStartingPosition = newitem.Key;
                         NewSpintaxListDict.Add(newitem.Key, olditem.Value);
                         //}
                         break;
                     }
                 }                        
             }
         }

         Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary<int, SpintaxMappedValue>(NewSpintaxListDict);
     }            
 }

这就是我在做的......

此字典键是字符串

中每个单词的索引

请帮帮我 提前谢谢!

3 个答案:

答案 0 :(得分:0)

字符串在C#中是不可变的。这意味着一旦设置它们就不会改变。当您再次设置变量时,实际上是在分配一个新字符串,并清除旧字符串(如果不再引用)。

字符串必须从您的对象中取出才能达到您的要求,我建议每次请求时使用字符串构建器生成属性。您还可以使用脏标志来查看字典更改时是否需要生成它。

答案 1 :(得分:0)

如果你要替换句子中的单词,我会选择基于string.Replace()的方法

originalString = originalString.Replace("man","women and man");

通过这种方式,您无需关心使用不断变化的索引交换子字符串。

答案 2 :(得分:0)

var original = "A man meet a man";

//updated in response to comment
//var dict = Regex.Matches(original, @"\b\w+\b").Cast<Match>().ToDictionary(m => m.Index, m=> m.Value);

var dict = Regex.Matches(original, @"\b\w+\b")
    .Cast<Match>()
    .GroupBy(m => m.Value, m=> m.Index)
    .ToDictionary(g => g.Min(), g=> g.Key);

当字符串更改时,重新创建字典。

使用字典:

string s = dict[2];
Console.WriteLine(s=="man"); //prints True

dict[2] = "new string";
Console.WriteLine(dict[2]=="new string"); //prints True