Does your company or startup need an expert? If so, write to me to talk about possibilities of professional cooperation.
PROGRAMMING on-line course is approaching! Write to me if you are interested in free early access.

MARCIN.com

Marcin Jamro, PhD, DSc

How to get alphanumeric key from string?

C#  |  .NET

There is sometimes the necessity to generate an alphanumeric key from a string, such as to generate a part of the URL based on the article title. You need to make all letters lower-case and replace some language-specific characters, as well as replace all characters other than a-z and 0-9 with a dash. What's more, no more than one dash in a row is allowed. How to solve this problem?

The most important actions are performed in the following method:

string GetKey(string text)
{
    // Replace some language-specific characters.
    Dictionary replace = new()
    {
        { 'ą', 'a' },
        { 'ć', 'c' },
        { 'ę', 'e' },
        { 'ł', 'l' },
        { 'ń', 'n' },
        { 'ó', 'o' },
        { 'ś', 's' },
        { 'ź', 'z' },
        { 'ż', 'z' }
    };
    text = text.ToLower();
    foreach ((char from, char to) in replace) { text = text.Replace(from, to); }

    // Replace all not-supported characters with dashes.
    List supported = [];
    for (char c = 'a'; c <= 'z'; c++) { supported.Add(c); }
    for (char c = '0'; c <= '9'; c++) { supported.Add(c); }
    string key = string.Empty;
    foreach (char c in text) { key += supported.Contains(c) ? c : '-'; }

    // Remove multiple dashes, as well as dashes at the beginning or end.
    key = key.Replace("--", "-");
    key = key.Trim('-');
    return key;
}

To simplify testing, the Print auxiliary method is shown below:

void Print(string text)
{
    Console.Write(text);
    Console.Write(" -> ");
    Console.WriteLine(GetKey(text));
}

A few of its calls are presented as follows:

Print("Hello world!");
Print("Dzień dobry w przepięknej Polsce! :-)");
Print("I was born on 9.11.1988.");

If you run the code, you will get the following result:

Hello world! -> hello-world
Dzień dobry w przepięknej Polsce! :-) -> dzien-dobry-w-przepieknej-polsce
I was born on 9.11.1988. -> i-was-born-on-9-11-1988

The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.

Hello, I am Marcin

Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.

Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.

Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.

You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.

Books

I am an author of a few books and numerous publications, also in high-quality international scientific journals.

If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.

Projects

Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com

Do you follow your favorite creators? If so, check it out what products they recommend and buy them at: https://refspace.com

Travels

I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.