简体   繁体   中英

How to generate a string with less than / more than length

I would like to allow users to choose the exact length, or less than / more than count o characters to be generated (in a password generator).

For example i have done:

// the longivity of the generated string  
var stringChars = new char[int.Parse(TextBox2.Text)];
var random = new Random();

for (int i = 0; i < stringChars.Length; i++)
{
  // abc has been declared before, it is simply ABCD... for character generation
  stringChars[i] = abc[random.Next(abc.Length)];
}

var finalString = new String(stringChars);
// this is the result box
TextBox1.Text = finalString;

Now my problem is if user enters for eg. 10 and wants less than 10 characters of generated string, what should I do?

You could do for example something like this (with the power of Linq):

public string GeneratePassword(string abc, int minLenght, int maxLenght)
{
    var random = new Random();
    var chars = Enumerable
        .Range(0, random.Next(minLenght, maxLenght + 1)) // Generate a range between the min and max.
        .Select(x => abc[random.Next(abc.Length)])       // Select a random character from the abc.
        .ToList();

    // Concatenate the string.
    return string.Join(string.Empty, chars);
}

You have to using the System.Linq

Usage:

var input = 10;
var abc = "abcde";
var maxPasswordLength = 100;
var minPasswordLength = 1;

// More than the input.
GeneratePassword(abc, input + 1, maxPasswordLength);
// Less than the input.
GeneratePassword(abc, minPasswordLength, input - 1);
// Exact length.
GeneratePassword(abc, input, input);
public class Program
{
    private const int MAX_LENGTH = 50;
    static void Main(string[] args)
    {
        var less = GeneratedLessThan(12);
        var more = GeneratedMoreThan(12);
        Console.WriteLine($"Less Than : {less} ({less.Length})");
        Console.WriteLine($"More Than : {more} ({more.Length})");
        Console.ReadLine();
    }

    static char[] ABC()
    {
        List<char> list = new List<char>();
        for (char i = 'A'; i <= 'Z'; i++)
        {
            list.Add(i);
        }
        return list.ToArray();
    }

    static string GeneratedLessThan(int max) => GeneratedString(0, max);
    static string GeneratedMoreThan(int min) => GeneratedString(min, MAX_LENGTH);
    static string GeneratedString(int min, int max)
    {
        StringBuilder builder = new StringBuilder();
        var abc = ABC();
        var rnd = new Random();
        for (int i = 0; i <= rnd.Next(min, max); i++)
        {
            builder.Append(abc[rnd.Next(abc.Length)]);
        }

        return builder.ToString();
    }
}

Maybe you dont need ABC function.

Ouput :

Less Than : IORS (4)
More Than : BSVPFZVQRWZTSPYDI (17)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM