如何编写一个if语句来检查输入是否有字母 [英] How to write an if statement that checks if input has any letters

查看:51
本文介绍了如何编写一个if语句来检查输入是否有字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式,当输入中存在字母时,它可以使我成为true.我已经尝试过 if(formula.contains("[a-zA-Z] +")== true),但这似乎不起作用.

I need a regex that gives me true when a letter exists in the input. I have tried if (formula.contains("[a-zA-Z]+") == true), but this does not seem to work.

嗨!我想到了! formula.matches(.* [a-zA-Z].*")这行得通!

Hi! I figured it out! formula.matches(".*[a-zA-Z].*") this worked!

推荐答案

为此,您需要 String#matches(regex)和正确的当然

You'll need String#matches(regex) for this and a correct regex of course

if (formula.matches(".*[a-zA-Z].*")) {
    // do stuff
}

上面的正则表达式表示

.             Any character
 *            Zero or multiple times
  [a-zA-Z]    One letter in ranges a-z or A-Z
          .   Any character
           *  Zero or multiple times

这篇关于如何编写一个if语句来检查输入是否有字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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