简体   繁体   English

如何在C#中生成所有可能的3个字符的字符串?

[英]How to generate all possible 3-chars strings in c#?

The question is : 问题是 :

How to generate a list of all possible 3-chars strings in C#? 如何在C#中生成所有可能的3个字符的字符串的列表?

To clairify, that's within the domain of 64 10 and 90 10 要澄清一下,在64 10和90 10的范围内

IEnumerable<string> GetAllStrings(params char[] inputCharacterSet) {
    return from n in inputCharacterSet
           from m in inputCharacterSet
           from k in inputCharacterSet
           select new string(new [] { n, m, k });
}
public IEnumerable<String> Get3CharStrings(char[] domain)
{
    foreach(char a in domain)
     foreach(char b in domain)
      foreach(char c in domain)
       yield return "" + a + b + c;
}

EDIT: This is actually quite a bit slower than the LINQ solution posted by Mehrdad, although most of the difference lies in the use of return "" + a + b + c instead of return new string(new[] { a, b, c}) . 编辑:这实际上比Mehrdad发布的LINQ解决方案要慢很多,尽管大多数区别在于使用return "" + a + b + c而不是return new string(new[] { a, b, c})

Actual statistics (26-character alphabet, 10k iterations: 实际统计信息(26个字符的字母,10000次迭代:

Mehrdad's code: 72.983 seconds Mehrdad的代码:72.983秒
My code: 127.205 seconds 我的代码:127.205秒
My code with Mehrdad's return statement: 75.055 seconds 我的代码带有Mehrdad的return语句:75.055秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM