【问题标题】:c# string split and combinec# 字符串拆分和合并
【发布时间】:2011-05-23 08:35:41
【问题描述】:

我有一个包含 5 个数字的字符串,例如

'1,4,14,32,47'

我想用这个字符串制作 5 个字符串,每个字符串有 4 个数字

喜欢:

 '1,4,14,32'
 '1,4,14,47'
 '1,4,32,47'
 '1,14,32,47'
 '4,14,32,47'

什么是简单/快速的方法

是每次不同输入和组合时将其转换为数组未设置的方式 他们回到字符串?

有简单的方法吗?

谢谢

【问题讨论】:

  • 我怀疑你的第 4 行有一个类型。它应该是 '1,14,32,47' 而不是 '1,4,32,47'

标签: c# arrays string combinations


【解决方案1】:

怎么样

string s = "1,4,14,32,47";
string r = String.Join(",", s.Split(',').Where((x, index) => index != 1).ToArray());

【讨论】:

  • 你不是说...index != x...吗?
  • 不,index 是数组中的索引。 0..4
  • 它看起来不错,我从 0-4 运行第 2 行,每次都得到正确的字符串
【解决方案2】:

使用string.Split() 可以创建一个字符串数组。循环遍历它,以便在每次循环迭代中指示应跳过哪个元素(在第一次遍历时,忽略第一个元素,在第二次遍历时,忽略第二个元素)。

在该循环内,创建一个新数组,其中包含除您要跳过的元素之外的所有元素,然后使用 string.Join() 创建每个结果。

【讨论】:

    【解决方案3】:

    看看:

    http://msdn.microsoft.com/en-us/magazine/ee310028.aspx 在这里,您将找到 F# 中的示例,该示例将在组合和排列中提供正确的背景(这就是您所需要的)。还有代码,我觉得用C#翻译很容易

    Example of Code in C# (text in Italian but code in English)

    【讨论】:

    • 太棒了! +1 表示他实际上是在尝试在字符串中生成字符排列。
    【解决方案4】:

    对于那些需要更通用算法的人,这里有一个给出 m 项的 n 个长度子集的算法:

    private void GetPermutations()
    {
        int permutationLength = 4;
        string s = "1,4,14,32,47";
        string[] subS = s.Split(',');
        int[] indexS = new int[permutationLength];
    
        List<string> result = new List<string>();
        IterateNextPerm(permutationLength, indexS, subS, result);
    
        // Result will hold all your genberated data.
    }
    
    private void IterateNextPerm(int permutationLength, int[] pIndexes, string[] subS, List<string> result)
    {
        int maxIndexValue = subS.Count() - 1;
    
        bool isCorrect = true;
        for (int index = 0; index < permutationLength - 1; index++)
        {
            if (pIndexes[index] >= pIndexes[index + 1])
            {
                isCorrect = false;
                break;
            }
        }
    
        // Print result if correct
        if (isCorrect)
        {
            string strNewPermutation = string.Empty;
            for (int index = 0; index < permutationLength; index++)
            {
                strNewPermutation += subS[pIndexes[index]] + ",";
            }
            result.Add(strNewPermutation.TrimEnd(','));
        }
    
        // Increase last index position
        pIndexes[permutationLength - 1]++;
    
        // Check and fix if it's out of bounds
        if (pIndexes[permutationLength - 1] > maxIndexValue)
        {
            int? lastIndexIncreased = null;
    
            // Step backwards and increase indexes
            for (int index = permutationLength - 1; index > 0; index--)
            {
                if (pIndexes[index] > maxIndexValue)
                {
                    pIndexes[index - 1]++;
                    lastIndexIncreased = index - 1;
                }
            }
    
            // Normalize indexes array, to prevent unnecessary steps
            if (lastIndexIncreased != null)
            {
                for (int index = (int)lastIndexIncreased + 1; index <= permutationLength - 1; index++)
                {
                    if (pIndexes[index - 1] + 1 <= maxIndexValue)
                    {
                        pIndexes[index] = pIndexes[index - 1] + 1;
                    }
                    else
                    {
                        pIndexes[index] = maxIndexValue;
                    }
                }
            }
        }
    
        if (pIndexes[0] < maxIndexValue)
        {
            IterateNextPerm(permutationLength, pIndexes, subS, result);
        }
    }
    

    我知道这不是最好的编码,但我现在在过去半小时内就写好了,所以我确定那里有一些事情要做。

    玩得开心!

    【讨论】:

      【解决方案5】:
      var elements = string.Split(',');
      
      var result =
      Enumerable.Range(0,elements.Length)
      .Reverse()
      .Select(
       i=>
        string.Join(","
         Enumerable.Range(0,i).Concat(Enumerable.Range(i+1,elements.Length - i - 1))
         .Select(j=>elements[j]).ToArray() // This .ToArray() is not needed in NET 4
        )
      ).ToArray();
      

      【讨论】:

        【解决方案6】:

        你的措辞很混乱......但这个例子很清楚。只需 split 逗号,然后 remove 一个索引,然后使用 string.Join(",", list);把它重新组合起来..

        【讨论】:

          猜你喜欢
          • 2016-03-23
          • 1970-01-01
          • 2021-07-17
          • 1970-01-01
          • 2021-06-10
          • 2017-06-16
          • 2011-02-13
          • 2011-11-25
          相关资源
          最近更新 更多