我将一个字符串(长度为3-8)分配给一个变量(text)。我想检查第二个和第三个字符是否不是数字(字母或符号或space..or数字以外的任何东西)。
发布于 2014-02-10 12:10:53
基本的方法可能是:
if(((text.charAt(1)-'0')>=0)&&(text.charAt(1)-'0')<10))||((text.charAt(2)-'0')>=0)&&(text.charAt(2)-'0')<10)))
{
//do nothing, since this means 2nd and/or 3rd characters in the string are numeric
}
else
{
// Your condition is met
}如果你的检查仍然很复杂,你也可以使用REGEX。
发布于 2014-02-10 12:12:01
这是另一种实现这一点的方法:
boolean isNumeric = true;
String test = "testing";
char second = test.charAt(1);
char third = test.charAt(2);
try {
Integer.parseInt(String.valueOf(second));
Integer.parseInt(String.valueOf(third));
} catch(NumberFormatException e) {
isNumeric = false;
}
System.out.println("Contains Number in 2nd and 3rd or both position: " + isNumeric);发布于 2014-02-10 12:16:19
您可以使用String.IndexOf(String)方法,如下所示:
String digits = "0123456789";
String s2 = text.substring(2,3);
String s3 = text.substring(3,4);
boolean valid = (digits.indexOf(s2) > -1) && (digits.indexOf(s3) > -1);https://stackoverflow.com/questions/21668875
复制相似问题