簡體   English   中英

比較C#中的字符列表

[英]Compare List of characters in C#

我正在嘗試創建一種方法來檢查字符串是否包含相同的字符,以下是我的代碼:

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

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

        return ' ';
    }

我創建了一個列表,將所有字符保存到列表中,我的想法是檢查門框的第一個索引是否與下一個索引相等,然后返回該字符。 我的問題是如何比較同一列表中的字符? 我是否需要另一個從索引j = 1開始的for循環,然后將其與列表中的上一個索引進行比較? 還是有其他更好的解決方案?

注意:我不想使用Linq

您根本不需要列表,可以像訪問字符數組一樣訪問字符串中的字符:

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

注意:該方法返回一個可為null的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;
}

如果您要查找連續字符的首次出現,即對我來說,這還不是很清楚。 “ 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;
}

如果您只想檢查不帶空格的字符,則可以使用此代碼,此代碼返回在字符串內部找到的第一個相同字符。

    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 ' ';

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM