I am working on project where User have to insert character between A to AZ. Ex. If User Enter Values Like This
Field 1= AA
Field 2= AD
Then I have to generate AA,AB,AC,AD.
CodePudding user response:
string start = "AD", end = "BC";
int p1 = int.Parse(string.Concat((int)start[0], (int)start[1]));
int p2 = int.Parse(string.Concat((int)end[0], (int)end[1]));
var range = Enumerable.Range('A', 26);
var ret = string.Join(",", from r in range
from r2 in range
where int.Parse(string.Concat(r, r2)) >= p1
&& int.Parse(string.Concat(r, r2)) <= p2
select string.Concat((char)r, (char)r2));
Console.WriteLine(ret);
Output: AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ,BA,BB,BC
Explanation:
Take the numeric value of each character in a pair and string concatenate them, e.g.
"AD" = "65" "68" = 6568 (lower bound)
"BC" = "66" "67" = 6667 (upper bound)
From a list of generated pairs AA to ZZ, compare the numeric value using the same formula above to get the desired result
"AA" = "65" "65" = 6565 (outside bound)
"AB" = "65" "65" = 6566 (outside bound)
...
"AD" = "65" "68" = 6568 (inside bound)
...
"BD" = "66" "68" = 6668 (outside bound)
CodePudding user response:
A simple nested loop should do it. Something like:
var alphabets = new List<string>();
for(int i = 65;i< 65 26;i )
for(int j = 65;j< 65 26;j )
alphabets.Add($"{(char)i}{(char)j}");
CodePudding user response:
This should work to get the values you are expecting
public static void combination() {
string[] letters = { "a", "b", "c", "d"... };
List<string> combinations = new List<string>();
for (int i = 0; i < letters.Length; i )
{
for (int j=0; j<letters.Length; j )
{
combinations.Add(letters[i] letters[j]);
}
}
}
CodePudding user response:
Finally issue is solved. Sharing my answer For Angular Code. Hope it will help others too.
Function to Convert Number To Char
numberToCol(num) {
var str = '', q, r;
while (num > 0) {
q = (num - 1) / 26;
r = (num - 1) % 26
num = Math.floor(q)
str = String.fromCharCode(65 r) str;
}
return str;
}
Function To Convert Char To Number
GenerateAlphabeticArray(letter1, letter2) {
let var1 = letter1.split('').reduce((r, a) => r * 26 parseInt(a, 36) - 9, 0);
let var2 = letter2.split('').reduce((r, a) => r * 26 parseInt(a, 36) - 9, 0);
let a = [];
for (let i = var1; i <= var2; i ) {
a.push(this.numberToCol(i))
}
console.log("AtoZData = ", a);
return a;
}
Make a call of fuction
this.GenerateAlphabeticArray('AA', 'AD');
Output
['AA', 'AB', 'AC', 'AD']