如何检查字符串是否至少包含1个字母字符?

Nul*_*ead 1 c# asp.net string random

我的ASP.NET应用程序要求我生成大量的随机字符串,每个字符串至少包含1个字母和数字字符,并且整体上应该是字母数字.为此,我的逻辑是如果随机字符串是数字,则再次生成代码:

        public static string GenerateCode(int length)
        {
            if (length < 2 || length > 32)
            {
                throw new RSGException("Length cannot be less than 2 or greater than 32.");
            }
            string newcode = Guid.NewGuid().ToString("n").Substring(0, length).ToUpper();
            return newcode;
        }

        public static string GenerateNonNumericCode(int length)
        {
            string newcode = string.Empty;
            try
            {
                newcode = GenerateCode(length);
            }
            catch (Exception)
            {
                throw;
            }
            while (IsNumeric(newcode))
            {
                return GenerateNonNumericCode(length);
            }
            return newcode;
        }

        public static bool IsNumeric(string str)
        {
            bool isNumeric = false;
            try
            {
                long number = Convert.ToInt64(str);
                isNumeric = true;
            }
            catch (Exception)
            {
                isNumeric = false;
            }
            return isNumeric;
        }
Run Code Online (Sandbox Code Playgroud)

在调试时,它工作正常但是当我要求它创建10,000个随机字符串时,它无法正确处理它.当我将该数据导出到Excel时,我发现平均至少有20个数字字符串.这是我的代码或C#的问题? - 我的.
如果有人在寻找代码,

public static string GenerateCode(int length)
    {
        if (length < 2)
        {
            throw new A1Exception("Length cannot be less than 2.");
        }
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var random = new Random();
        var result = new string(

        Enumerable.Repeat(chars, length)
                  .Select(s => s[random.Next(s.Length)])
                  .ToArray());

    return result;
}

public static string GenerateAlphaNumericCode(int length)
{
    string newcode = string.Empty;
    try
    {
        newcode = GenerateCode(length);
        while (!IsAlphaNumeric(newcode))
        {
            newcode = GenerateCode(length);
        }
    }
    catch (Exception)
    {
        throw;
    }

    return newcode;
}

public static bool IsAlphaNumeric(string str)
{
    bool isAlphaNumeric = false;
    Regex reg = new Regex("[0-9A-Z]+");
    isAlphaNumeric = reg.IsMatch(str);
    return isAlphaNumeric;
}
Run Code Online (Sandbox Code Playgroud)

感谢您的所有想法.

Dea*_*nOC 6

如果你想坚持使用Guid作为生成器,你总是可以使用正则表达式进行验证如果至少存在一个alpha,则只会返回true

Regex reg = new Regex("[a-zA-Z]+");
Run Code Online (Sandbox Code Playgroud)

然后只需使用IsMatch方法查看您的字符串是否有效

这样你就不需要(恕我直言,相当丑陋)try..catch围绕转换.

更新:我看到你后来关于实际使你的代码变慢的评论.您是仅仅实例化Regex对象一次,还是每次进行测试?如果后者那么这将是相当低效的,你应该考虑在你的班级上使用"懒惰"属性,例如

    private Regex reg;

    private Regex AlphaRegex
    {
        get
        {
            if (reg == null) reg = new Regex("[a-zA-Z]+");
            return reg;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后在您的方法中使用AlphaRegex.IsMatch().我希望这会有所作为.


use*_*269 5

使用名称空间,然后使用System.Linq; 使用普通字符串检查字符串是否至少包含一个字符或数字。

using System.Linq; 

string StrCheck = "abcd123";
check the string has characters --->  StrCheck.Any(char.IsLetter) 

check the string has numbers   --->   StrCheck.Any(char.IsDigit)

if (StrCheck.Any(char.IsLetter) && StrCheck.Any(char.IsDigit))
{
//statement goes here.....
}

sorry for the late reply ...
Run Code Online (Sandbox Code Playgroud)