Skip to content

Instantly share code, notes, and snippets.

@romulets
Created November 14, 2016 12:16
Show Gist options
  • Save romulets/c40796f191415e2c916218772b49dfa6 to your computer and use it in GitHub Desktop.
Save romulets/c40796f191415e2c916218772b49dfa6 to your computer and use it in GitHub Desktop.
Increments a string like Excel [A ... AA ... AAA ... AAAA]
class StringIncrementable
{
public static string Increment(string strFrom)
{
char[] letters = new char[strFrom.Length + 1];
int i;
for (i = 0; i < strFrom.Length; i++)
letters[i] = strFrom[i];
for (i = letters.Length - 2; i >= 0; i--)
{
if (letters[i] == 'Z')
{
letters[i] = 'A';
if (i > 0 && letters[i - 1] != 'Z')
{
letters[i - 1]++;
break;
}
else if (i == 0)
{
for (int j = 0; j < letters.Length; j++)
letters[j] = 'A';
break;
}
}
else
{
letters[i]++;
break;
}
}
return new string(letters).Trim('\0');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment