Introduction
This class provides a method to count up a string. What does that mean? Well, let us assume we have a string containing the word „HELLO“. When we „increment“ this string it would hold the word „HELLP“, then „HELLQ“, then „HELLR“, and so on.
Background
You may know the most simple use case for such a class, which would be a brute-force tool.
In my case I had to generate unique and consecutive alpha-numeric IDs.
Using the code
The simplest way to use the code is to create a new instance of the AlphaCountUp class and call the Next()
method. After that the CurrentWord
property holds the „incremented“ string value.
AlphaCountUp acu = new AlphaCountUp();
acu.Next();
The empty constructor defines a character set containing
- Upper case letters
ABCDEFGHIJKLMNOPQRSTUVWXYZ - Lower case letters
abcdefghijklmnopqrstuvwxyz - Numbers
0123456789 - Special characters
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
These single character sub-sets can directly be accessed by the static fields .UpperCaseLetters
, .LowerCaseLetters
, .Numbers
and .SpecialChars
.
There are two ways to define an instance's charater set:
- By using the appropriate constructor
- By setting the
Charset
property
The character set can contain any character a string can hold, as long as it is unique; that means a character set like „ABCDEFA“ would be invalid, due to the letter „A“ is contained twice. In this case an ArgumentException
is thrown. This type of exception is also thrown when the character set does not contain all characters the current word contains. E.g. the current word is „XYZ“ and the Charset
property should be set to „ABC“.
The current word can be set or retrieved using the CurrentWord
property. When a word shall be set, which contains characters not part of the current character set, an ArgumentException
is thrown.
To „increment“ the string, stored in the CurrentWord
property the Next()
method is called.
To restore the initial values of the character set and word, that have been assigned by calling the constructor, the Reset()
method can be called.
How does it work?
By default the used character set is initialized with upper and lower case letters, numbers and special characters, the current/initial word is set to blank.
When the Next()
method is called it checks whether the current word is different from blank. If not the current word is set to the first character of the currently defined character set. If the word is different from blank the position of the word's last character within the charset is looked up.
Then this character is checked. If it is equal to the last character in the charset, the first current word except the last character is recursively incremented and the first character of the charset attached to the result. If the character to check is not equal to the last character in the charset, then it is replaced by the next character in it.
The new word is stored in the CurrentWord
property and also returned by the Next()
method.
The following code is the "core" of the described class. It shows the private method to increment a string.
private string Next(string currentWord)
{
if (string.IsNullOrEmpty(currentWord))
{
return _charset[0].ToString();
}
var searchChar = currentWord[currentWord.Length - 1];
var charPosition = _charset.IndexOf(searchChar);
if (charPosition < 0)
throw new ArgumentException("The word contains characters which are not part of the Charset.");
if (charPosition == _charset.Length - 1)
{
return Next(currentWord.Substring(0, currentWord.Length - 1)) + _charset[0];
}
else
{
return currentWord.Substring(0, currentWord.Length - 1) + _charset[charPosition + 1];
}
}
Further examples
The following example creates a new instance of AlphaCountUp, setting the character set to upper case letters and initializes the current word with "TEST". Then this word is incremented 256 times and the single results are written to the console.
AlphaCountUp acu = new AlphaCountUp(AlphaCountUp.UpperCaseLetters);
acu.CurrentWord = "TEST";
for (int i = 0; i < 256; i++)
{
word = acu.Next();
Console.WriteLine(word);
}
Intitializing the used character set and word can also be done in the constructor:
AlphaCountUp acu = new AlphaCountUp(AlphaCountUp.UpperCaseLetters, "TEST");
Another example, also demonstrating the classes performance, can be found in the project file. The following screenshot shows the generated output after 10 million iterations and the initial word "Test".
Points of Interest
If you want to clear the current word and character set, the CurrentWord
property needs to be set to blank before the Charset
property is cleared, otherwise you will run into an exception.
History
- 2006 - Initial version.
- January 26th, 2015 - Minor changes