【问题标题】:Compare List of characters in C#比较 C# 中的字符列表
【发布时间】:2016-01-31 18:37:16
【问题描述】:

我正在尝试创建一种方法来检查字符串是否包含相同的字符,以下是我的代码:

public char GetCommonChar(string text)
    {
        List<char> myList = new List<char>();

        for (int i = 0; i < text.Length; i++)
        {
            myList.Add(text[i]);
        }

        return ' ';
    }

我创建了一个列表来将所有字符保存到列表中,我的想法是检查 linst 的第一个索引是否与下一个索引相等,然后返回该字符。 我的问题是如何比较同一列表中的字符?我是否需要另一个从索引 j=1 开始的 for 循环,然后将其与列表中的前一个索引进行比较?还是有其他更好的解决方案?

注意:我不想使用 Linq

【问题讨论】:

  • 你想做什么比较字符串的前两个字符,如果前两个字符相同,则返回该字符串,否则返回 null ?给出示例字符串

标签: c# list char compare


【解决方案1】:

你根本不需要列表,你可以像访问字符数组一样访问字符串中的字符:

public char? GetCommonChar(string text) {
  for (int i = 1; i < text.Length; i++) {
    if (text[i] != text[0]) return null;
  }
  return text[0];
}

注意:该方法返回一个可以为空的char,因此如果字符不同,它可以返回一个null值。

编辑:

要找到字符串中出现频率最高的字符,您需要计算每个字符的数量。

一种方法是循环遍历每个字符并计算该字符的数量,然后跟踪出现次数最多的字符:

public char GetCommonChar(string text) {
  char result = ' ';
  int max = 0;
  foreach (char c in text) {
    int cnt = text.Count(i => i == c);
    if (cnt > max) {
      result = c;
      max = cnt;
    }
  }
  return result;
}

另一种方法是为每个字符设置一个计数器,然后在计算完所有字符后找到最高的计数器:

public char GetCommonChar(string text) {
  Dictionary<char, int> cnt = new Dictionary<char, int>();
  foreach (char c in text) {
    if (cnt.ContainsKey(c)) {
      cnt[c]++;
    } else {
      cnt.Add(c, 1);
    }
  }
  char result = ' ';
  int max = 0;
  foreach (KeyValuePair<char, int> item in cnt) {
    if (item.Value > max) {
      result = item.Key;
      max = item.Value;
    }
  }
  return result;
}

【讨论】:

  • 对 :),好主意
  • 但是兄弟在字符串参数为“AABBMGR B”的情况下,例如,您的代码将不起作用..
  • @Kob_24:它会返回null,因为字符串中的所有字符都不是相同的。这就是我解释的函数应该做的事情,如果你想要别的东西,那么你必须解释它是什么。
  • 是的,是的,我只是想让它变得更复杂,只是为了学习 :) thx 为你的重播.. 在这个例子中的 okej 可以说它应该返回“B”中重复的字符细绳。所以我必须创建一个计数器来计算更多重复的字符不是吗?
  • @Kob_24:您需要的不仅仅是一个计数器,还需要计算字符串中每个字符出现的次数,并找出出现次数最多的字符。我在上面添加了两个示例。
【解决方案2】:

如果你想找到一个连续字符的第一次出现,我并不完全清楚,即。 “hello”中的“l”,或整个字符串中的第一个重复字符,即。 “abca”中的“a”。

如果第一个连续字符是你想要的:

private char? FirstConsecutiveCharacter(string input)
{
    for (var i = 1; i < input.Length; ++i)
    {
        if (input[i] == input[i - 1])
        {
            return input[i];
        }
    }
    return null;
}

如果整个字符串中第一个重复的字符是你想要的:

private char? FirstRepeatedCharacter(string input)
{
    var knownCharacters = new HashSet<char>();
    for (var i = 0; i < input.Length; ++i)
    {
        // Add returns true when the value is new, false when the value already exists in the hashset.
        if (!knownCharacters.Add(input[i]))
        {
            return input[i];
        }
    }
    return null;
}

【讨论】:

    【解决方案3】:

    如果你想只检查不带空格的字符,你可以使用这个,这个代码返回第一个相同的字符,在字符串中找到。

        static void Main(string[] args)
        {
    
            string text = "check this sample";
    
            char x = GetCommonChar(text);
            Console.WriteLine(x.ToString());
            Console.ReadKey();
    
    
        }
    
        public static char GetCommonChar(string text)
        {
            List<char> myList = new List<char>();
    
            for (int i = 0; i < text.Length; i++)
            {
                if(!string.IsNullOrWhiteSpace(text[i].ToString()))
                myList.Add(text[i]);
            }
    
            int fpoint = 0;
            int spoint = 0;
            while (fpoint < myList.Count-1)
            {
               while (spoint < myList.Count-1)
               {
                   if ((myList[fpoint] == myList[spoint + 1]) && (fpoint!=spoint+1))
                       return myList[fpoint];
                   spoint++;
               }
                spoint = 0;
                fpoint++;
            }
    
            return ' ';
    
        }
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2019-03-14
      • 2023-04-06
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 2020-12-18
      • 2014-05-09
      • 1970-01-01
      相关资源
      最近更新 更多