I would suggest using regular expression for this because of two reasons. First is that what happens when user input something like A12B7
or A1BC4
? Your approach will fail because you're assuming that every letter will have only one digit following it.
Second reason is pure readability. It's easier to filter everything out using regular expressions than doing it by hand, character by character.
So first define your regex like this one: (?'pair'[A-Za-z]{1}d{0,})
Having this expression will retrieve pairs that have letter and possibly a number following. Then you can iterate through all of the pairs and just create new string for each.
string result = "";
foreach(string pair in pairs)
{
char character = pair[0];
int length = 1;
if (pair.Length > 1)
{
length = int.Parse(pair.Substring(1));
}
result += new String(character, length);
}
Console.WriteLine(result);
You can test this online on rextester
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…