【问题标题】:How to check if a string has at least 1 alphabetic character? [duplicate]如何检查字符串是否至少有 1 个字母字符? [复制]
【发布时间】:2011-10-13 00:38:43
【问题描述】:

我的 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;
        }

在调试时,它工作正常,但是当我要求它创建 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;
}

感谢大家的想法。

【问题讨论】:

  • 只是猜测,但可能是你,而不是 C#。
  • 我看不出代码有什么问题。好吧,实际上,生成大量随机字符串非常慢,但我稍后会谈到。与此同时,你真的有你的代码来生成 10,000 个字符串吗?我想看看。这对我来说似乎工作得很好。

标签: c# asp.net string random


【解决方案1】:

除了字母(abc 等)之外,我不太明白您在字符串中想要什么 - 比如说数字。
您可以生成如下随机字符:

Random r = new Random();
r.Next('a', 'z'); //For lowercase
r.Next('A', 'Z'); //For capitals
//or you can convert lowercase to capital:
char c = 'k' + ('A' - 'a');


如果要创建字符串:

var s = new StringBuilder();
for(int i = 0; i < length; ++i)
    s.Append((char)r.Next('a', 'z' + 1)); //Changed to char
return s.ToString(); 


注意:我不太了解 ASP.NET,所以我只是把它当作 C#。

【讨论】:

  • 请注意,您必须将r.Next 的结果转换为char,否则附加到字符串的值仍然是数字而不是字母。
  • 另外,Random.Next 的上限是独占的,因此如果您希望 z 可能出现在字符串中,则需要在该上限上加 1。我想您可以随机选择是否生成随机的小写字母、大写字母或数字。
  • @Quantic:我需要一个字母数字字符串,而不仅仅是字母。
  • @Reddy 好吧,你付给我的钱不够我让你完成所有工作
【解决方案2】:

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

Regex reg = new Regex("[a-zA-Z]+");

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

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

更新:我看到了您随后关于实际上使您的代码变慢的评论。您是只实例化一次 Regex 对象,还是每次测试完成时实例化一次?如果是后者,那么这将是相当低效的,您应该考虑在您的类上使用“延迟加载”属性,例如

    private Regex reg;

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

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

【讨论】:

  • @Reddy S R:请回复您的慢评论。我看到您的代码每次都会创建 Regex 对象。尝试延迟加载属性以加快速度。
【解决方案3】:

要严格回答您的问题,请使用您现有的代码:您的递归逻辑存在问题,可以通过不使用递归来避免(在@987654321 中绝对没有理由使用递归@)。请改为执行以下操作:

    public static string GenerateNonNumericCode(int length)
    {
        string newcode = GenerateCode(length);
        while (IsNumeric(newcode))
        {
            newcode = GenerateCode(length);
        }
        return newcode;
    }

其他一般说明

您的代码效率非常低——抛出异常的成本很高,因此在循环中使用 try/catch 既慢又没有意义。正如其他人所建议的那样,正则表达式更有意义(System.Text.RegularExpressions 命名空间)。

是我的代码或 C# 有问题吗?

如果有疑问,问题几乎从来都不是 C#。

【讨论】:

  • 附带说明,我已将代码更改为使用 Regex 而不是 try/catch,并注意到该应用程序花费了 10 秒的时间。
【解决方案4】:

所以,我将代码更改为:

static Random r = new Random();
public static string GenerateNonNumericCodeFaster(int length) {
    var firstLength = r.Next(0, length - 1);
    var secondLength = length - 1 - firstLength;
    return GenerateCode(firstLength)
         + (char) r.Next((int)'A', (int)'G')
         + GenerateCode(secondLength);
}

您可以保持 GenerateCode 函数不变。其他你扔掉的东西。这里的想法当然是,而不是测试字符串是否包含字母字符,您只需显式输入一个。在我的测试中,使用此代码可以在 0.0172963 秒内生成 10,000 个 8 个字符串,而您的代码大约需要 52 秒.所以,是的,这大约快 3000 倍 :)

【讨论】:

    【解决方案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 ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 2019-12-29
      • 2020-03-26
      • 2022-11-16
      相关资源
      最近更新 更多