比如 string str="ABC"
要的结果:  A,B,C,AB,AC,BC  要的这种形式的。 不是BAC,ACB.....那种。。
M

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "ABC";
                for (int i = 1; i < 1 << str.Length; i++)
                    Console.WriteLine(string.Join("", str.Select((x, idx) => (i | (1 << idx)) == i ? x.ToString() : "")));
            }
        }
    }A
    B
    AB
    C
    AC
    BC
    ABC
    Press any key to continue . . .随机产生一个:
    string str = "ABC";
    int i = new Random().Next(1, 1 << str.Length);
    Console.WriteLine(string.Join("", str.Select((x, idx) => (i | (1 << idx)) == i ? x.ToString() : "")));AC
    Press any key to continue . . .
    (每次不同)
      

  2.   


            static void Main(string[] args)
            {
                Test("ABC");
            }
            public class ABC
            {
                public int Index;
                public string Str;
            }
            static void Test(string s)
            {
                int size = s.Length;
                Stack<ABC> stack = new Stack<ABC>();
                stack.Push(new ABC { Index = 0, Str = "" });            while (stack.Count > 0)
                {
                    ABC abc = stack.Pop();
                    for (int i = abc.Index; i < size; i++)
                    {
                        Console.WriteLine(abc.Str + s[i]);
                        stack.Push(new ABC() { Index = i + 1, Str = abc.Str + s[i] });
                    }
                }
            }
      

  3.   

        static class Program
        {
            static void Main(string[] args)
            {
                foreach (string s in GetEnumerable())
                {
                    Console.WriteLine(s);
                }
            }
            static IEnumerable GetEnumerable()
            {
                string abc = "ABCDE";
                int size = abc.Length;
                for (int s = 1; s < size; s++)
                {
                    for (int i = 0; i < size - s + 1; i++)
                    {
                        string ss = abc.Substring(i, s);
                        yield return ss;
                    }
                }
            }
        }