从一个字符串中返回第一个没有重复出现的字符(例如"Hello World",返回"H")
1 public static string GetTheFirstUniqueCharOfString(string str)
2 {
3 if (string.IsNullOrEmpty(str)) return str;
4 IDictionary<int, int> iDictionary = new Dictionary<int, int>();
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 }
2 {
3 if (string.IsNullOrEmpty(str)) return str;
4 IDictionary<int, int> iDictionary = new Dictionary<int, int>();
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 }
作者:舍长
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.