In Java, determine whether a string is "both numeric" and "contains numbers" and "intercepts numbers" (GO)

Source: Internet
Author: User

    1. http://cherryqq.iteye.com/blog/406355

      In JavaScript, there is a method isdigit () used to determine whether a string is a number, in the Java string processing method does not have such a method, it is often necessary to use, so the Internet search, sorted out two with regular expression matching method of judgment, as follows;

      Java code
      1. Determines whether a string is numeric
      2. Public Boolean isdigit (String strnum) {
      3. return Strnum.matches ("[0-9]{1,}");
      4. }
      5. Determines whether a string is numeric
      6. Public Boolean isdigit (String strnum) {
      7. Pattern pattern = Pattern.compile ("[0-9]{1,}");
      8. Matcher Matcher = Pattern.matcher ((charsequence) strnum);
      9. return matcher.matches ();
      10. }
      11. Intercept numbers
      12. public string Getnumbers (string content) {
      13. Pattern pattern = Pattern.compile ("\\d+");
      14. Matcher Matcher = pattern.matcher (content);
      15. While (Matcher.find ()) {
      16. return Matcher.group (0);
      17. }
      18. return "";
      19. }
      20. Intercept non-digital
      21. public string Splitnotnumber (string content) {
      22. Pattern pattern = Pattern.compile ("\\d+");
      23. Matcher Matcher = pattern.matcher (content);
      24. While (Matcher.find ()) {
      25. return Matcher.group (0);
      26. }
      27. return "";
      28. }
      29. Determines whether a string contains a number
        public boolean hasdigit (String content) {
        Boolean flag = false;
        Pattern p = pattern.compile (". *\\d+.*");
        Matcher m = p.matcher (content);
        if (M.matches ())
        Flag = true;
        return flag;
        }
      Three ways to judge whether a string is a number in Java
      1 functions in Java
      public static Boolean isnumeric (String str) {
      for (int i = Str.length ();--i>=0;) {
      if (! Character.isdigit (Str.charat (i))) {
      return false;
      }
      }
      return true;
      }

      2 Using regular expressions
      public static Boolean isnumeric (String str) {
      Pattern pattern = Pattern.compile ("[0-9]*");
      return Pattern.matcher (str). matches ();
      }

      3 with ASCII code

      public static Boolean isnumeric (String str) {
      for (int i=str.length ();--i>=0;) {
      int Chr=str.charat (i);
      if (chr<48 | | chr>57)
      return false;
      }
      return true;
      }

In Java, determine whether a string is "both numeric" and "contains numbers" and "intercepts numbers" (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.