Help, How do i get string values in pairs and execute a function with it?

I’ll try to explain this as much as I can.

I am working on a personal plugin that requires use of letter combinations such as aa ab ac … zx zy zz. I want to be able to create a public string and let the user put text in the inspector while the code breaks them up in pairs and execute a function.

PSEUDO CODE:

  • User types “very bad” in the inspector
  • string pairs will be(.toUpper) VE RY BA DD (d+d last unpaired pairs with itself)
  • if or switch statements do: if string(VE){something;} if string RY {something;}

** I have already set all the possible pairs in the alphabet as functions. I am not sure about adding that last pair, if it’s possible great :slight_smile: but I really need to convert sentences to alphabet pairs for this to work. Thanks in advance.

//Remove white spaces and group the string
string[] groups = input.Replace(" ", "").ToCharArray().Select((c, i) => new {character = c, index = i}).GroupBy(a => a.index / 2).Select(grp => new string(grp.Select(g => g.character).ToArray()))).ToArray();
//Iterate over them
foreach (string group in groups) {
//Append the character if it is only one letter
group += group.Length == 1 ? group : string.Empty;
//Switch
switch (group) {
...
}
}

Grouping by the index divided by 2 works because integer division loses the fractional part of the result. Thus:

0 / 2 ==
1 / 2 ==
0;

2 / 2 ==
3 / 2 ==
1; //and so on.

Oh and you need LINQ to make this magic work.