2015-12-03 105 views
1

我在C#中有一个已排序的字典,其中的日期以“NA”,“WD”,“LD”,“HD”等字符串加入。看起来像。如何替换某个文件中的某个特定数字

2015年10月1日NA

2015年10月2日NA

2015年10月3日NA

2015年10月4日LD

2015年10月5日WD

2015年10月6日WD

2015年10月7日HD

2015年10月8日WD

2015年10月9日LD

2015年10月10日高清

二○一五年十月十一日WD

2015年10月12日高清

2015年10月13日高清

2015年10月14日NA

2015年10月15日NA

2015年10月16日WD

二○一五年十月一十七日HD

2015年10月18日WD

现在,处理这些信息后,我会有如下结果。

10月1日/ 2015NA 3

10月2日/ 2015NA 3

10月3日/ 2015NA 3

10/04/2015LD 3

10月5日/ 2015WD 2

/04/2 2015WD

10月7日/ 2015HD 1 //我应该得到这改变为2。

10/08/2015WD 2

10/09/2015LD 3

10/10/2015HD 1

10/11/2015WD 2

10/12/2015HD 1 //没有改变。因为它不出现在2.

10/13/2015HD 1 //对此没有任何改变。因为它不出现在2之间。

14分之10/ 2015NA 3

10/15/3 2015NA

10/16/2 2015WD

17分之10/ 2015HD 1 //我应该得到这改变为2。

10月18日/ 2015WD 2

我需要更改号码1 2,我尽我所能去解决它之间的到来。但似乎没有任何工作。经过近6个小时的努力,我绝望了。由于这是编程,所以肯定有一个解决方案,但无法实现。

请问谁能指导我。

+0

该字典的关键是什么?处理后的数据仍然在字典中? –

+0

该字典定义为:SortedDictionary sortedDictionary = new SortedDictionary ();数据将以文本文件形式提供。我只需要最后一个号码用于我的目的。 – Unnikrishnan

+0

我使用sd.Key和sd.Value.using foreach循环来检索信息。 – Unnikrishnan

回答

0

这会适合你吗?

SortedDictionary<string, string> Dates = new SortedDictionary<string, string>(); 
Dates.Add("1", "10/06/2015WD 2 "); 
Dates.Add("2", "10/07/2015HD 1"); 
Dates.Add("3", "10/08/2015WD 2 "); 
for(int i = 0; i < Dates.Count; i++) 
{ 
    string Key = Dates.ElementAt(i).Key; 
    string CurrentValue = Dates.ElementAt(i).Value.Trim(); 
    string CurrentValueLastChar = CurrentValue.Substring(CurrentValue.Length - 1, 1); 
    if (i - 1 != -1 && i + 1 < Dates.Count) 
    { 
     string PreviousValue = Dates.ElementAt(i - 1).Value.Trim(); 
     string NextValue = Dates.ElementAt(i + 1).Value.Trim(); 
     string PreviousValueLastChar = PreviousValue.Substring(PreviousValue.Length - 1, 1); 
     string NextValueLastChar = NextValue.Substring(NextValue.Length - 1, 1); 
     if (PreviousValueLastChar == NextValueLastChar) 
      Dates[Key] = (Dates[Key].Remove(Dates[Key].Length - 1)) + PreviousValueLastChar; 

    }     

} 
+0

我正在一个接一个地尝试。我不是Waqar Ahmed先生和Backtrac先生这样的专家。谢谢。 – Unnikrishnan

+0

您对我提出的问题的解决方案工作正常。这是分享你的知识的一个有趣的部分。我没有话语​​要谢谢你。 – Unnikrishnan

+0

是的,这绝对是对我有用@Waqar Ahmed。 – Unnikrishnan

相关问题