How to get continuous characters in C #?

I have

List<String> MyList=new List<string>(); 

I need to populate MyList n values.

if n is 2, then the MyList will contain

 "A","B" 

if 10 then

 "A","B","C"....."J" 

if 30 then

 "A"....."Z","AA","AB",AC","AD" 

if 1000 then

 "A",....."Z","AA","AB"......"AZ","BA","BB"......."BZ"........"YZ","AAA",AAB"..... and so on 

I do not know how to do that.

Please help me do this using any method. Using LINQ or LAMBDA Expression

+6
c # lambda linq
source share
6 answers

Edit 2 :

This is probably the easiest way to implement it . I tested it, it works fine. You can create an infinite number of lines.

 public IEnumerable<string> GenerateStrings() { foreach(string character in Alphabet()) { yield return character; } foreach (string prefix in GenerateStrings()) { foreach(string suffix in Alphabet()) { yield return prefix + suffix; } } } public IEnumerable<string> Alphabet() { for(int i = 0; i < 26; i++) { yield return ((char)('A' + i)).ToString(); } } 

Material I wrote earlier:

You can also write a small recursive function that returns any string with a specific index. This may not be optimal in performance because there are several duplicate sections, but it may be fast enough for your purpose.

This is pretty short and easy:

 string GetString(int index) { if (index < 26) { return ((char)('A' + index)).ToString(); } return GetString(index / 26 - 1) + GetString(index % 26); } 

(can also be placed in another method:

 List<string> strings = Enumerable.Range(0, 1000) .Select(x => GetString(x)) .ToList(); 

This is working code, just wrote a test for it.


Edit: for example, the application "full linq way" from GetString:

 public void IEnumerale<string> GenerateStrings() { int index = 0; // generate "infinit" number of values ... while (true) { // ignoring index == int.MaxValue yield return GetString(index++); } } List<string> strings = GenerateStrings().Take(1000).ToList(); 
+6
source share

This is similar to this question (but not quite enough to mark it as a duplicate, and this is a difficult search problem anyway).

Use any of IEnumerable<string> working answers (or at least any that covers the required range), and then if you need to create a list with a certain number of elements, just use:

 List<string> list = GenerateSequence().Take(count).ToList(); 
+3
source share

This code is working fine, but I'm not sure if it is "LINQ enough."

 char[] validChars = Enumerable.Range(0, 26).Select(i => (char)('A' + i)).ToArray(); List<string> result = new List<string>(); List<string> generator = validChars.Select(ch => ch.ToString()).ToList(); int n = 1000; while (result.Count < n) { result.AddRange(generator); generator = generator.Take((n - result.Count) / validChars.Length + 1) .SelectMany(s => validChars.Select(ch => s + ch)) .ToList(); } var output = result.Take(n); 
+3
source share

I did something similar in SQL a while ago.

Translated to C #, this is a function to create code from among:

 public static string GetCode(int id) { string code, chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (id <= chars.Length) { code = chars.Substring(id - 1, 1); } else { id--; int value = chars.Length, adder = 0; while (id >= value * (chars.Length + 1) + adder) { adder += value; value *= chars.Length; } code = chars.Substring((id - adder) / value - 1, 1); id = ((id - adder) % value); while (value > 1) { value /= chars.Length; code += chars.Substring(id / value, 1); id = id % value; } } return code; } 

Then you just get the numbers from 1 to and translate into codes:

 var codes = Enumerable.Range(1, 1000).Select(n => GetCode(n)); 

The function limit is currently "ZZZZZZ" or 321272406. (After that, you get division by zero.)

Note that this function uses all combinations and returns "A" .. "Z", "AA" .. "ZZ", "AAA" ... "ZZZ" instead of starting with "AB" and "ABC ",.

+3
source share

Try the following. I use Cross Join and Union to create a source, and then to filter the record using the Take extension method

 char[] charArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); List<String> MyList = new List<string>(); int n = 1000; (from value1 in charArray select new { newString = value1.ToString() }) .Union ( (from value1 in charArray from value2 in charArray select new { newString = string.Concat(value1, value2) }) ) .Union ( (from value1 in charArray from value2 in charArray from value3 in charArray select new { newString = string.Concat(value1, value2, value3) }) ) .Take(n) .ToList() .ForEach(i => MyList.Add(i.newString)); 

Hope this gives you some insight into using a combination of Linq, Lambda and Extension.

+1
source share

@DannyChen is he. your code with minor changes.

 char[] validChars = Enumerable.Range(0, 26).Select(i => (char)('A' + i)).ToArray(); int n = 30; int pointer = 0; int pointerSec = 0; int Deg = 0; string prefix = string.Empty; string prefixMore = string.Empty; List<string> result = new List<string>(); while (n > 0) { result.AddRange(validChars.Skip(pointer).Select(ch => prefix + ch).Take(n)); if (pointer == 26) { pointer = -1; Deg += 1; prefixMore = "" + validChars[pointerSec]; pointerSec++; n++; } else { if (Deg == 0) { prefix = "" + validChars[pointer]; } else { prefix = prefixMore + validChars[pointer]; } } n--; pointer++; } 

he is 100% right.

0
source share

All Articles