关于字符串的问题在于,可以在字符串中以多种不同格式提供数值,其中一些格式永远不会被数字解析器(如Integer.parseInt() 或Double.parseDouble())传递。下面的 isNumeric() 方法可以处理相当多的最常见的方法,至少是在北美使用的方法。需要对String#matches() 方法中以下isNumeric() 方法代码中使用的Regular Expression 进行一些修改,以处理世界其他地区使用的其他特定数字符号格式。
isNumeric() 方法:
/**
* Returns true if the string is a numerical type value.
*
* The Regular Expression (regex) used here covers just about any provided
* numerical values like:<pre>
*
* "05", "0", "18", "-12", "-33.0", "0.33", "-22.8", ".675E8",
* "-.322", "44.343e6", "+453", "+94.234, 1,234.87, 32,043,001"
* </pre>
*
* @param numericalString (String)
*
* @return (boolean)
*/
public static boolean isNumeric(String numericalString) {
/* Utilization of the String#matches() method.
The String#replace() method is used to remove commas in numbers.
These commas should also be removed before parsing to a specific
numerical data type (wherever that might be in your code). */
return numericalString.replace(",", "").matches("[-+]?(\\d+)?(\\.(\\d+)?((?i)E\\d+)?)?");
}
如何使用 isNumeric() 方法:
// The string to work against.
String fullString = "Some 3 random 5.6 text 12 with -4.5 numbers -1 "
+ "in 0 between. These are added 05 0 18 -12 -33.0 0.33 "
+ "-22.8 .675E8 -.322 44.343e6 +453 +94.234 1,234.87 "
+ "32,043,001 as well!";
/* Split the contents of fullString based on one or more
whitespaces as delimiter. This takes care of any strings
that may have double-spacing anywhere within it. */
String [] words = fullString.split("\\s+");
/* Because we may never know how large any given string may
be, We use the List Interface or ArrayList object to
create a collection of both words and double numerical
values. Either of these object can grow dynamically (as
can may other objects). Arrays can not grow dynamically.
You can convert these Lists to Arrays later if you really
like. */
List<String> wordsList = new ArrayList<>();
List<Double> numbersList = new ArrayList<>();
/* Iterate through the 'words' array and add alpha character
words to the 'wordsList' list and numerical values (if any)
to the 'numbersList' list. In this example all string number
values will be converted to Double data type (also handles
Exponents). */
for (String wordStrg : words) {
if (isNumeric(wordStrg)) {
// The String#replace() method id used to remove commas in numbers.
numbersList.add(Double.parseDouble(wordStrg.replace(",", "")));
}
else {
wordsList.add(wordStrg);
}
}
// Show what is in the Lists:
System.out.println("The Words List:");
System.out.println(wordsList);
System.out.println();
System.out.println("The Numbers List (as Double):");
System.out.println(numbersList);
// ==============================================================
// If for some reason you want to convert the Lists
// to arrays then you can do it this way:
String[] onlyWords = wordsList.toArray(new String[wordsList.size()]);
Double[] onlyNumbers = numbersList.toArray(new Double[numbersList.size()]);
// Display The Arrays:
System.out.println();
System.out.println("The Words Array:");
System.out.println(Arrays.toString(onlyWords));
System.out.println();
System.out.println("The Numbers Array (as Double):");
System.out.println(Arrays.toString(onlyNumbers));