I'm trying:
String string = "123456";
if(string.startsWith("[0-9]") && string.endsWith("[0-9]")){
//code
}
And the if clause is never called.
I'm trying:
String string = "123456";
if(string.startsWith("[0-9]") && string.endsWith("[0-9]")){
//code
}
And the if clause is never called.
Don't use a regex:
Character.isDigit(string.charAt(0)) &&
Character.isDigit(string.charAt(string.length()-1))
(see Character.isDigit())
IllegalStateException. Try checking the surrounding code.The methods startsWith() and endsWith() in class String accept only String, not a regex.
You can use the matches method on String thusly:
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$"));
}
Output:
true
false
false
false
^\\d.*?\\d$ for a string, it does not work. For string 3rd it should give false and for strings 3 or 3(white spaces) the output should be true. Any guidance would be much appreciated.You can use:
String string = "123test123";
if(string.matches("\\d.*\\d"))
{
// ...
}
matches().