[C#] RandomCustomStrings

The RandomCustomStrings class in the Promezio.RandomCustomStrings namespace provides a simple yet flexible way to generate random strings in C#. This can be highly useful in scenarios like generating random IDs, passwords, test data, and more. The class allows for extensive customization through the StringConfig class, enabling users to specify the length and character types of the generated strings.

Project on GitHub

NuGet package

Features

  • Customizable String Length: Define the length of the generated string.
  • Character Type Inclusion: Opt to include uppercase, lowercase, numbers, special characters, and/or custom characters in the string.
  • Custom Character Support: Add any set of custom characters to the string generation pool.

Usage

Add Namespace: Include the namespace in your code.

using Promezio.RandomCustomStrings;

Create Configuration: Instantiate a StringConfig object with your desired settings.

StringConfig config = new StringConfig(
    lenght: 10, 
    containUppercase: true, 
    containLowercase: true, 
    containNumbers: true, 
    containSpecialChars: true
);

Generate String: Use the Generate method to create a random string.

string randomString = RandomCustomStrings.Generate(config);

Example

StringConfig config = new StringConfig(15, containUppercase: true, containNumbers: true);
string myRandomString = RandomCustomStrings.Generate(config);
Console.WriteLine(myRandomString);

Included seeds

uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
lowercase = "abcdefghijklmnopqrstuvwxyz";
numbers = "0123456789";
specialChars = ",.;:-_*()%urltomarkdowncodeblockplaceholder40.08828644441564792?!=/";

Use custom characters

StringConfig config = new StringConfig(10,
    customChars: "@£\|",
    containCustomChars: true);

string randomString = RandomCustomStrings.Generate(config);

You'll only receive email when they publish something new.

More from GSLF
All posts