接受大写和小写字母字符

时间:2018-07-21 21:26:16

标签: java boolean uppercase

我试图弄清楚如何接受下面代码中大写和小写字母的用户输入。我的应用程序输入了用户名的字符串,并且无论大小写如何,我都需要让他们知道该字符是否在其中。

boolean p=str.contains("R");
if(p)
System.out.println("string contains the char 'R'");
else
System.out.println("string does not contains the char 'R'");

1 个答案:

答案 0 :(得分:-1)

您可以通过调用toUpperCase()简单地转换您的输入,该方法将返回所有大写字母的字符串,然后将contains应用于返回的类型

boolean p = str                // robot
               .toUpperCase()  // ROBOT
               .contains("R"); // ROBOT contains R? true/false
// note: value of str is still robot as original

尽管如果您不需要多次使用p,则无需执行其他字符检查

if(str.toUpperCase().contains("R"))
    System.out.println("string contains the char 'R'");
else
    System.out.println("string does not contains the char 'R'");
相关问题