如何检查字符串中的每个字符

时间:2016-12-13 18:56:01

标签: java

正如我在上面的标题中所提到的,我需要知道如何检查String中的每个字符,如下所示:

  1. 检查字符串是否小于3个字符。
  2. 检查字符串是否包含数字。
  3. 检查单个字符串是否没有元音。
  4. 我一直在寻找确切的例子,但我找不到一个。

1 个答案:

答案 0 :(得分:1)

按照以下步骤操作:

String s = "mystring";
s.length() < 3 // expression which says if it's smaller than 3.
s.matches(".*[0-9].*") //expression which says if there are digits in the string
s.matches("\b[^aeiouAEIOU ]+\b") //expression which says if there are no vovels

检查yourString是否符合所有条件:

if (myString.length() < 3 && myString.matches(".*[0-9].*") 
    && myString.matches("\b[^aeiouAEIOU ]+\b")) {
   //matches all
}
相关问题