字符串至少包含一个数字 [英] String contains at least one digit

查看:131
本文介绍了字符串至少包含一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看字符串是否至少包含一个数字或小写字母或大写字母。

I am trying to see whether a string contains at least a digit or a lowercase or an uppercase.

我写过这样的内容:

      int combinations = 0;
      string pass = "!!!AAabas1";

      if (pass.matches("[0-9]")) {
          combinations = combinations + 10;
      }

      if (pass.matches("[a-z]")) {
          combinations =combinations + 26;
      }

      if (pass.matches("[A-Z]")) {
          combinations =combinations + 26;
      }

但是我不明白为什么我不能得到组合去36。他们只是保持在0.我做错了什么?

However I don't understand why I cannot get combinations to go to 36. They just remain at 0. What am I doing wrong?

推荐答案

您可以使用Pattern代替,我认为匹配方法看起来使整个字符串与正则表达式匹配。

You could use Pattern instead, I think "matches" method looks for the whole string to match the regular expression.

尝试下一个代码:

    int combinations = 0;
    String pass = "!!AAabas1";
    if (Pattern.compile("[0-9]").matcher(pass).find()) {
        combinations = combinations + 10;
    }

    if (Pattern.compile("[a-z]").matcher(pass).find()) {
        combinations = combinations + 26;
    }

    if (Pattern.compile("[A-Z]").matcher(pass).find()) {
        combinations = combinations + 26;
    }

这篇关于字符串至少包含一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆