我正在尝试:
String string = "123456";
if(string.startsWith("[0-9]") && string.endsWith("[0-9]")){
//code
}
if
语句从未被执行。
不要使用正则表达式:
Character.isDigit(string.charAt(0)) &&
Character.isDigit(string.charAt(string.length()-1))
见Character.isDigit()
。IllegalStateException
。请尝试检查周围的代码。 - arshajii在String
类中,方法startsWith()
和endsWith()
只接受字符串,而不是正则表达式。
String
的matches
方法:public static void main(String[] args) throws Exception {
System.out.println("123456".matches("^\\d.*?\\d$"));
System.out.println("123456A".matches("^\\d.*?\\d$"));
System.out.println("A123456".matches("^\\d.*?\\d$"));
System.out.println("A123456A".matches("^\\d.*?\\d$"));
}
输出:
true
false
false
false
^\\d.*?\\d$
时,它不起作用。 对于字符串3rd
,它应该返回false
,而对于字符串3
或3(空格)
,输出应该为true
。 任何指导都将不胜感激。 - user12736831^\\d.*?\\d$
时,它不起作用。对于字符串3rd
,它应该返回false
,而对于字符串3
或3(空格)
,输出应该是true
。非常感谢任何指导。 - user12736831您可以使用:
String string = "123test123";
if(string.matches("\\d.*\\d"))
{
// ...
}
matches()
方法时实际上不需要锚点。 - arshajiiString variableString = "012testString";
Character.isDigit(string.charAt(0)) && variableString.Any(c => char.IsUpper(c));