Array of strings to judge numbers, alphabetic characters,

Source: Internet
Author: User

A method in C + + that determines whether a string is numeric:
cout <<"Please enter an array of integers (no more than 1000):"<<Endl
+ cin >> arr[0];
While (GetChar ()! = '\ n')
{
CIN >> Arr[num];
num++;
+ }

A method in Java that determines whether a string is a number: 1. The Java-brought function public static Boolean isnumeric (String str) {for (int i = 0; i < str.length (); i++) { System.out.println (Str.charat (i)); if (! Character.isdigit (Str.charat (i))) {return false; }} return true; }2. Use regular Expressions first to import Java.util.regex.Pattern and Java.util.regex.Matcherpublic boolean isnumeric (String str) {Pattern Patt Ern = Pattern.compile ("[0-9]*"); Matcher isnum = Pattern.matcher (str); if (!isnum.matches ()) {return false; } return true; }3. Using Org.apache.commons.langorg.apache.commons.lang.stringutils;boolean isnunicodedigits=stringutils.isnumeric ( "aaa123456789"); http://jakarta.apache.org/commons/lang/api-release/index.html the following explanation: Isnumericpublic static Boolean isnumeric (String str) Checks If the string contains only Unicode digits. A decimal point was not a Unicode digit and returns False.null would return false. An empty String ("") would return true. Stringutils.isnumeric (NULL) = False Stringutils.isnumeric ("") = True StringutiLs.isnumeric ("") = False Stringutils.isnumeric ("123") = True Stringutils.isnumeric ("3") = False Stringutils.isnume Ric ("AB2C") = False Stringutils.isnumeric ("12-3") = False Stringutils.isnumeric ("12.3") = False parameters:str-the Strin G to check, could be null returns:true if only contains digits, and is non-null above three ways, the second way is more flexible. The first to third way can only check the number without the minus sign "-", that is, enter a negative-199, the output will be false, and the second way you can modify the regular expression to implement the check negative, the regular expression is modified to "^-?" [0-9]+], modified to "-?" [0-9]+.? [0-9]+] to match all numbers.

Array of strings to determine numbers, alphabetic characters,

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.