Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Alphanumeric count up

0.00/5 (No votes)
27 Jan 2015 1  
Class providing methods to count up a string using a definable character set.

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

Image 1

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.

C#
// Absolutely minimized call.
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:

  1. By using the appropriate constructor
  2. 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.

C#
private string Next(string currentWord)
{
    // If no current word is set, set it to the first character of the Charset.
    if (string.IsNullOrEmpty(currentWord))
    {
        return _charset[0].ToString();
    }

    // Get last character of current word.
    var searchChar = currentWord[currentWord.Length - 1];

    // Get position of searched char within charset.
    var charPosition = _charset.IndexOf(searchChar);

    // Character found in charset?
    if (charPosition < 0)
        throw new ArgumentException("The word contains characters which are not part of the Charset.");

    // If found position equals length of charset then replace the
    // last character of the current word with the first character
    // of the charset and add new character to the new word.

    // Last character in charset?
    if (charPosition == _charset.Length - 1)
    {
        // Yes, set last character of current word to first character
        // in charset and check the rest of the string.
        return Next(currentWord.Substring(0, currentWord.Length - 1)) + _charset[0];
    }
    else
    {
        // No, set last character of current word to next character
        // within charset.
        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.

C#
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:

C#
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".

Image 2

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here