1

我正在尝试做的是验证一些输入,以便输入必须以 2 个字母开头,后跟 3 个数字,但我找不到为此测试字符串的方法

 boolean test;
 String str;
 str.format ("%s%s%d%d%d") //used this to give what the format was and was going to use it with a boolean, tried it in an if statement such as

if str.format = ("%s%s%d%d%d") then
{
 test = true
}
else
{
 test = false
}

我想知道我必须做些什么来实现这一目标?

4

1 回答 1

2

听起来你想要一个正则表达式......

// Do this once and cache it...
Pattern pattern = Pattern.compile("\\p{Alpha}{2}\\d{3}");

// Then test it:
boolean test = pattern.matcher(str).lookingAt();

有关更多详细信息(以及 的替代方法),请参阅Pattern文档\p{Alpha}

于 2012-01-20T15:46:44.110 回答