简体   繁体   English

创建包含空格(字符之间的一个空格)的随机字符串,第一个字符不是空格

[英]Create random string that includes spaces (one space between characters), and first character is not a space

I have a method that creates a random string that includes spaces with a user-specified length. 我有一个方法创建一个随机字符串,其中包含用户指定长度的空格。 The problem is, is that sometimes it inserts spaces at the beginning of the string, and it creates consecutive spaces. 问题是,有时它会在字符串的开头插入空格,并创建连续的空格。 How do I combat this so my string never has any spaces in the first character but still can have spaces at other indexes (non-consecutive). 我如何解决这个问题,所以我的字符串在第一个字符中永远不会有任何空格,但仍然可以在其他索引处有空格(非连续)。 Keep in mind that I want to be exact with the sizeOfString parameter, so I need the end result to still be a user-specified amount of characters 请记住,我希望与sizeOfString参数完全匹配,因此我需要最终结果仍然是用户指定的字符数量

This is my method: 这是我的方法:

    public static string CreateRandomString(int sizeOfString)
    {
        const string chars = "A b  C  D  e  F  g  H  I  1  2  3  4  5  6  7  8  ";

        var random = new Random();
        return new string(Enumerable.Repeat(chars, sizeOfString)
          .Select(s => s[random.Next(s.Length)]).ToArray());
    }

You could create an infinite sequence of random chars, skip while they are spaces and then take the number of characters you want. 你可以创建一个无限的随机字符序列,当它们是空格时跳过,然后取你想要的字符数。

If you declare a new random within your function, then if you call the function too frequently your random strings get repeated. 如果在函数中声明一个新的随机数,那么如果你过于频繁地调用该函数,则会重复随机字符串。

Also, the frequency of spaces is very high as more than half of your characters are spaces, you could use chars = " AbCDeFgHI12345678" to alleviate this. 此外,空格的频率非常高,因为超过一半的字符是空格,你可以使用chars = " AbCDeFgHI12345678"来缓解这个问题。

private static Random random = new Random();

public static string CreateRandomString(int sizeOfString)
{
    const string chars = "A b  C  D  e  F  g  H  I  1  2  3  4  5  6  7  8  ";

    var randomChars =
        InitInfinite(() => chars[random.Next(chars.Length)])
            .SkipWhile(c => c == ' ')
            .Take(sizeOfString);

    return new string(randomChars.ToArray());
}

public static IEnumerable<T> InitInfinite<T>(Func<T> selector)
{
    while (true)
    {
        yield return selector();
    }
}

Edit - And here's version 2 (final version): 编辑 - 这里是版本2(最终版本):

public static string CreateRandomSentence(int sizeOfString)
{
    var sb = new StringBuilder();

    while (sb.Length < sizeOfString)
    {
        int wordLength = random.Next(8) + 1;
        sb.Append(CreateRandomString(wordLength)).Append(" ");
    }

    sb.Length = sizeOfString;

    return sb.ToString();
}

public static string CreateRandomString(int sizeOfString)
{
    const string chars = "AbCDeFgHI12345678";

    var randomChars =
        InitInfinite(() => chars[random.Next(chars.Length)])
            .SkipWhile(c => c == ' ')
            .Take(sizeOfString);

    return new string(randomChars.ToArray());
}

public static IEnumerable<T> InitInfinite<T>(Func<T> selector)
{
    while (true)
    {
        yield return selector();
    }
}

private static Random random = new Random();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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