2011-06-08 56 views
0

我需要将stings“a”“b”“c”“d”组合起来。 我试过把它们放在一个列表中,然后通过它们解析一个foreach方法,但无济于事。如何在C#中组合字符串?

我还能做什么?

+2

你想要什么结果呢? – 2011-06-08 09:23:32

+0

你的意思是连接它们? – 2011-06-08 09:23:32

+0

你是什么意思的“组合”? – yurib 2011-06-08 09:23:57

回答

0

我会给你的逻辑。

创建一个List<String>,然后继续添加每个字符串。

List<String> s = new List<String>(); 

s.add("a"); 
s.add("b"); 
s.add("c"); 
s.add("d"); 

一旦你添加的所有字符串,那么产生这样的最小值和最大值指数之间的随机数:

private int RandomNumber(int min, int max) 
{ 
Random random = new Random(); 
return random.Next(min, max); 
} 

然后,当打印在循环中的每个串在List这个数字, 确保检查相同的随机数不重复的nex迭代。

+0

如何将字符串添加到列表字符串? – Derek 2011-06-08 09:26:51

+0

列表是一个泛型类型,它只允许添加字符串,没有其他对象, – 2011-06-08 09:27:28

0

你会列出一个列表吗?

List<String>myStrings = new List<String>(); 

myStrings.Add("a"); 
... 
myStrings.Add("d"); 

你应该能够循环通过

+0

我试过了,无法让循环工作。 – Derek 2011-06-08 09:27:57

0

,如果你有一个字符串的一组号码,您可以使用

var s = String.Format("{0}{1}{2}{3}", stringA, stringB, stringC, stringD); 

否则为/ foreach循环将是未来的方向,

var sb = new StringBuilder(); 
var strings = new List<string>(); 

// Add strings to list 

for (var i = 0; i < strings.Count; i++) 
{ 
    sb.Append(strings[i]); 
} 

var s = sb.ToString(); 

我不会用字符串+字符串+由于字符串在内存中的工作方式,这是不好的做法。

编辑:我没有测试它在浏览器中写的代码!让我知道你是否有任何问题。

刚才看到上面的评论,我发布的代码将始终以相同的顺序输出字符串,所以可能不是你想要的。

HTH

单次

0
List<String>stringList = new List<String>(); 

stringList.Add("a"); 
stringList.Add("b"); 
... 
... 

foreach(string item in stringList) 
{ 
    string text=item; 
} 
3

让数组中的字符串可以使用下面的函数打印所有排列,因此输出:abcd,abdc,adbc等。

递归置换(从MSDN):

public static void Permute(string[] strings, int start, int finish) 
    { 
    if (start == finish) 
    { 
     for (int i = 0; i <= finish; ++i) 
     { 
     Console.Write(strings[i] + " "); 
     } 
     Console.WriteLine(""); 
    } 
    else 
    { 
     for (int i = start; i <= finish; ++i) 
     { 
     string temp = strings[start]; 
     strings[start] = strings[i]; 
     strings[i] = temp; 

     Permute(strings, start+1, finish); 

     temp = strings[start]; 
     strings[start] = strings[i]; 
     strings[i] = temp; 
     } 
    } 

    } // Permute() 
0

这个例子做了所有的组合(正确):

using System; 
using System.Linq; 
using System.Collections.Generic; 

public static class Program 
{ 
    public static void Main(string[] args) 
    { 
     var list = new [] { "a", "b", "c", "d" }; 
     foreach (var combi in Enumerable.Repeat(list, list.Length).CartesianProduct()) 
      Console.WriteLine(string.Join(" ", combi)); 
    } 

    static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) 
    { 
     IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; 
     return sequences.Aggregate( 
       emptyProduct, 
       (accumulator, sequence) => 
       from accseq in accumulator 
       from item in sequence 
       select accseq.Concat(new[] {item}));     
    } 
} 

输出:

a a a a 
a a a b 
a a a c 
a a a d 
a a b a 
a a b b 
a a b c 
a a b d 
a a c a 
a a c b 
.... 
d d b c 
d d b d 
d d c a 
d d c b 
d d c c 
d d c d 
d d d a 
d d d b 
d d d c 
d d d d 

如果您需要置换,你可以插入一个算法

  1. MoreLinq
  2. CodeProject上http://www.codeproject.com/KB/recipes/Combinatorics.aspx
相关问题