I have a project which requires me to generate multiple artifacts with sequential names. No sweat, right? Just stick a number on the end of the name and increment it. Well, what if your users prefer letters to numbers?
Ruby's string.succ function returns the successor to the string by incrementing characters starting from the rightmost alphanumeric character. It's a shame that C# doesn't have this built in.
Here's my stab at recreating the Ruby succ function in C#. It's not as robust as the ruby version, as it only handles letters, but it is a start and it meets my needs for now.
public static string Succ(string value)
List<string> alphabet = new List<string>(" ,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".Split(",".ToCharArray()));
char values = (" " + value).ToUpper().ToCharArray();
bool addone = true;
for(int i = values.Length - 1; i > -1; i--)
if (values[i] != 'Z')
values[i] = char.Parse(alphabet[alphabet.IndexOf(values[i].ToString()) + 1]);
addone = false;
values[i] = 'A';
return new string(values).Trim();
Here's some sample output:
- Succ("R") => "S"
- Succ("Z") => "AA"
- Succ("ZAZ") => "ZBA"
Song of the day:
A Moment of Clarity - Therapy? - Infernal Love