从一个字符串中返回第一个没有重复出现的字符(例如"Hello World",返回"H")

 1 public static string GetTheFirstUniqueCharOfString(string str)
 2 {
 3     if (string.IsNullOrEmpty(str)) return str;
 4     IDictionary<intint> iDictionary = new Dictionary<intint>();
 5     for (int i = 0; i < 256; i++)
 6     {
 7         iDictionary.Add(i, 0);
 8     }
 9     char[] chars = str.ToCharArray();
10     foreach (char c in chars)
11     {
12         iDictionary[(short)c] += 1;
13     }
14     foreach (char c in chars)
15     {
16         if (iDictionary[(short)c] == 1)
17         {
18             return c.ToString();
19         }
20     }
21     return str;
22 }

 

posted @ 2011-02-22 10:16  舍长  阅读(333)  评论(1编辑  收藏  举报