【问题标题】:Get all unique values out of strings从字符串中获取所有唯一值
【发布时间】:2018-11-22 06:31:43
【问题描述】:

我在从strings 中获取唯一值时遇到了一些问题。

例子:

string1 = "4,5"
string2 = "7,9"
string3 = "4,7,6,1"
string4 = "1"

在我需要以int 获取所有唯一值之后。在这种情况下,结果必须为 6。但是每次字符串的数量可以改变

这可能吗?

【问题讨论】:

  • unique value 是什么意思?
  • linqdistinct做一些研究
  • 他可能意味着不同值。
  • 不,我猜OP想要occurrence is one的值
  • OP 想要得到一个distinct 我相信的数字列表 - 即没有重复

标签: c# .net string


【解决方案1】:

使用SplitDistinct

var  input = "1,3,1,2,3,43,23,54,3,4";

var result input.Split(',')
                .Distinct();    

Console.WriteLine(string.Join(",",result));

输出

1,3,2,43,23,54,4

Full Demo Here


其他资源

String.Split Method

返回一个字符串数组,其中包含此实例中的子字符串 由指定字符串或 Unicode 的元素分隔的 字符数组。

Enumerable.Distinct Method

从序列中返回不同的元素。

【讨论】:

  • @JohnB OCD 确实:)
【解决方案2】:

如果“字符串的数量可以改变”,让我们将它们组织成一个集合

List<string> strings = new List<string> {
  "4,5",
  "7,9",  
  "4,7,6,1",
  "1"
};

那么我们就可以对一个简单的Linq

var uniques = strings
  .SelectMany(item => item.Split(',')) // split each item and flatten the result
  .Select(item => int.Parse(item))
  .Distinct()
  .ToArray(); // let's have an array of distinct items: {4, 5, 7, 9, 6, 1}

如果你想获得只出现一次的物品

var uniques = strings
  .SelectMany(item => item.Split(',')) // split each item and flatten the result
  .Select(item => int.Parse(item))
  .GroupBy(item => item) 
  .Where(item => item.Count() == 1)
  .Select(group => group.Key)
  .ToArray(); // let's have an array of items which appear once: {5, 9, 6} 

【讨论】:

  • 我喜欢这种投票方式,我也需要在当天使用我的投票
  • 最后一个很棒,+1
【解决方案3】:
  1. 您可以使用StringBuilder 的单个实例而不是使用字符串变量的数量

  2. 将所有元素转换为整数数组。

  3. 通过 Linq 获取仅出现一次的 Distinct/number

类似这样的:

    StringBuilder sb = new StringBuilder();
    sb.Append("5,5");
    sb.Append(",");
    sb.Append("7,9");
    sb.Append(",");
    sb.Append("4,7,6,1");
    sb.Append(",");
    sb.Append("1"); 

    string[] arr = sb.ToString().Split(',');
    int[] test = Array.ConvertAll(arr, s => int.Parse(s));


    var count = test
        .GroupBy(e => e)
        .Where(e => e.Count() == 1)
        .Select(e => e.First()).ToList();

output: 

9
4
6

POC:.netFiddler

【讨论】:

    【解决方案4】:

    一条线就可以完成这项工作

        string s = "1,3,1,2,3,43,23,54,3,4";
        string[] StrArry = s.Split(',');
    
        int[] IntArry = Array.ConvertAll(StrArry, int.Parse).Distinct().ToArray();
    

    输出

    1,3,2,43,23,54,4

    【讨论】:

      【解决方案5】:

      你可以试试这个

      var string1 = "4,5";
      var string2 = "7,9";
      var string3 = "4,7,6,1";
      var string4 = "1";
      
      var allStrings = string1 + ',' + string2 + ',' + string3 + ','+ string4;
      var distinctNumbers = new List<string>(allStrings.Split(',').Distinct());
      

      输出: 4 5 7 9 6 1

      distinctNumbers = 计数 = 6

      【讨论】:

        【解决方案6】:

        这比其他的更长,但它可能更容易理解它的工作原理。

                List<string> str = new List<string> {"1", "3", "1", "2", "3", "43", "23", "54", "3"," "4 };
        
                List<string> foundstr = new List<string> { };
        
                foreach (string check in str)
                {
                    bool found = false;
                    //going through every single item in the list and checking if it is found in there
                    for (int i = 0; i < foundstr.Count; i++)
                    {
                    //if found then make found(bool) true so we don't put it in the list
                        if(check == foundstr[i])
                        {
                            found = true;
                        }  
                    }
                    //checking if the string has been found and if not then add to list
                    if(found == false)
                    {
                        foundstr.Add(check);
                    }
                }
        
                foreach(string strings in foundstr)
                {
                    Console.WriteLine(strings);
                }
        
                Console.ReadLine();
        

        【讨论】:

          猜你喜欢
          • 2013-04-29
          • 1970-01-01
          • 2013-07-09
          • 1970-01-01
          • 2021-05-10
          • 1970-01-01
          • 2018-01-18
          • 1970-01-01
          • 2018-01-25
          相关资源
          最近更新 更多