Regex to test if a string ends with a number

后端 未结 5 1539
梦谈多话
梦谈多话 2021-01-11 10:28

I\'d like to test if a string ends with a a digit. I expect the following Java line to print true. Why does it print false?

System.out.println(\"I end with         


        
5条回答
  •  孤独总比滥情好
    2021-01-11 11:24

    System.out.println("I end with a number 4".matches(".*\\d")); // prints true
    

    or

    String s = "I end with a number 4";
    System.out.println(Character.isDigit(s.charAt(s.length()-1)));  // prints true
    

提交回复
热议问题