【问题标题】:Comparing a string with several different strings比较一个字符串和几个不同的字符串
【发布时间】:2010-01-15 07:11:58
【问题描述】:

我想将一个字符串与多个字符串进行比较。在 C# 中是如何实现的?

【问题讨论】:

  • 好的,在多次阅读这个问题以及 deepasundaris 自己的答案以及附加信息之后,我认为他想要以下内容:我有几个字符串的列表,需要找到一个未知的子字符串,该子字符串位于每个该列表中的字符串
  • 请重新表述问题以反映您的真正意思。
  • 您能否编辑您最初的问题以提供更多信息?我注意到您在这里澄清了一些 cmets 中的问题,但最好的方法是更新问题本身,这样人们就不必挖掘所有答案来了解您的需求。
  • 另外,这应该标记为与语言无关
  • 真的需要停止重新发布这个问题。如果有不清楚的地方,请编辑此问题以澄清您的意思。

标签: c# algorithm string


【解决方案1】:

如果要检查字符串是否包含在字符串列表中,可以使用Contains 扩展方法:

bool isStringContainedInList = 
    new[] { "string1", "string2", "string3" }.Contains("some string")

【讨论】:

  • 但问题是我不知道什么是“一些字符串”我想找到那个......怎么做?在许多字符串中找到共同点...
【解决方案2】:

我建议您查看此维基百科article,了解最长常见子字符串问题

我记得本科生有一个找到最长公共子串的策略,你可以从找到一个稍短的子串开始,然后从那里扩展(并重复)。也就是说,如果“abcd”是公共子串,那么“abc”和“ab”也是。

这有助于重复算法,您首先找到出现在字符串中的所有 2 字母对(我不关心一个字母的子字符串,因为对于大型数据集,它们将包含整个字母表)。然后您再次迭代以查找所有 3 个字母的子字符串,依此类推...

【讨论】:

    【解决方案3】:

    要将集合中的所有字符串相互比较以查找重复项,使用字典最有效:

    string[] strings = { "Zaphod", "Trillian", "Zaphod", "Ford", "Arthur" };
    
    var count = new Dictionary<string, int>();
    foreach (string s in strings) {
      if (count.ContainsKey(s)) {
        count[s]++;
      } else {
        count.Add(s, 1);
      }
    }
    foreach (var item in count) {
      Console.WriteLine("{0} : {1}", item.Key, item.Value);
    }
    

    输出:

    Zaphod : 2
    Trillian : 1
    Ford : 1
    Arthur : 1
    

    您也可以使用 LINQ 方法:

    var count =
      strings
      .GroupBy(s => s)
      .Select(
        g => new { Key = g.First(), Value = g.Count() }
      );
    

    【讨论】:

    • 不错的代码,但它并没有回答操作意图的问题
    • @Yoni:如果您知道 OP 的确切意图,请将其发布在此问题的某处。
    【解决方案4】:
     string[] comparisonList = {"a", "b" "c"};
     from s in comparisonList where comparisonList.Contains("b") select s;
    

    【讨论】:

    • 我认为这里不需要 LINQ。
    • @musicfreak:嗯。 @ash:LINQ 发布已经 4 年了。
    【解决方案5】:

    如果要比较,请使用String.Compare
    如果要在列表中查找字符串,请使用与列表类型等效的 Contains/Select 方法。

    【讨论】:

      【解决方案6】:

      我喜欢使用String.Compare() 静态方法,因为它可以让您将所有内容都明确化。这很重要,因为字符串比较可能会因细微的错误而臭名昭著。

      例如:

      // Populate with your strings
      List<string> manyStrings = new List<string>();
      
      string oneString="target string";
      
      foreach(string current in manyStrings)
      {
          // For a culture aware, safe comparison
          int compareResult=String.Compare(current,oneString,
                             StringComparison.CurrentCulture);
          // OR
          // For a higher performance comparison
          int compareResult=String.Compare(current,oneString,
                             StringComparison.Ordinal);
      
          if (compareResult==0) 
          {
              // Strings are equal 
      
          }
      }
      

      如果你真的想知道一个字符串是否是另一个更大字符串的子字符串,在上面的循环中你可以使用:

      int indexPos=current.IndexOf(oneString,StringComparison.Ordinal); 
      
      if (indexPos>=0)
      {
          // oneString was found in current
      }
      

      注意,IndexOf 接受同样有用的 StringComparison 枚举。

      【讨论】:

        【解决方案7】:

        要在列表中查找多次出现在列表中的字符串,您可以开始将这些字符串放入 HashSet 中,并检查每个字符串是否已经在此集合中。

        例如,您可以:

        HashSet<string> hashSet = new HashSet<string>();
        
        foreach (string item in myList)
        {
            if (hashSet.Contains(item)) 
            {
                // already in the list
                ...
            }
            else
            {
                // not seen yet, putting it into the hash set
                hashSet.Add(item);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-21
          • 1970-01-01
          • 1970-01-01
          • 2012-12-16
          • 2020-09-03
          • 1970-01-01
          相关资源
          最近更新 更多