ibcommons-lang-java 包中提供了一个类NumberUtils.java的isNumber()方法来验证一个string是否是numberic。现在分享出来。
public static boolean isNumber(String str) { if (StringUtils.isEmptyString(str)) { return false; } char[] chars = str.toCharArray(); int sz = chars.length; boolean hasExp = false; boolean hasDecPoint = false; boolean allowSigns = false; boolean foundDigit = false; // deal with any possible sign up front int start = (chars[0] == '-') ? 1 : 0; if (sz > start + 1) { if (chars[start] == '0' && chars[start + 1] == 'x') { int i = start + 2; if (i == sz) { return false; // str == "0x" } // checking hex (it can't be anything else) for (; i < chars.length; i++) { if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) { return false; } } return true; } } sz--; // don't want to loop to the last char, check it afterwords // for type qualifiers int i = start; // loop to the next to last char or to the last char if we need another digit to // make a valid number (e.g. chars[0..5] = "1234E") while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { if (chars[i] >= '0' && chars[i] <= '9') { foundDigit = true; allowSigns = false; } else if (chars[i] == '.') { if (hasDecPoint || hasExp) { // two decimal points or dec in exponent return false; } hasDecPoint = true; } else if (chars[i] == 'e' || chars[i] == 'E') { // we've already taken care of hex. if (hasExp) { // two E's return false; } if (!foundDigit) { return false; } hasExp = true; allowSigns = true; } else if (chars[i] == '+' || chars[i] == '-') { if (!allowSigns) { return false; } allowSigns = false; foundDigit = false; // we need a digit after the E } else { return false; } i++; } if (i < chars.length) { if (chars[i] >= '0' && chars[i] <= '9') { // no type qualifier, OK return true; } if (chars[i] == 'e' || chars[i] == 'E') { // can't have an E at the last byte return false; } if (chars[i] == '.') { if (hasDecPoint || hasExp) { // two decimal points or dec in exponent return false; } // single trailing decimal point after non-exponent is ok return foundDigit; } if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) { return foundDigit; } if (chars[i] == 'l' || chars[i] == 'L') { // not allowing L with an exponent return foundDigit && !hasExp; } // last character is illegal return false; } // allowSigns is true iff the val ends in 'E' // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass return !allowSigns && foundDigit; }
相关推荐
在JavaScript中,将字符串(string)转换为数字(number)是常见的操作,主要涉及到三种方法:`Number()`、`parseInt()`和`parseFloat()`。这些函数在不同的场景下有着不同的用途和限制。 1. `Number()` 函数: `...
Java中的`String.format()`方法是用于格式化字符串输出的一个强大工具。它允许程序员按照特定的模板格式化数据,包括数字、日期、时间和各种其他类型的对象。这个方法类似于.NET框架中的`System.String.Format()`...
// 或者 "Number is: " + num ``` 2. **字符串 --> 基本数据类型、包装类**: - 可以使用相应的包装类的 `parseXxx()` 方法,例如 `Integer.parseInt("100")`。 - 示例代码: ```java String s = "100"; int...
String strNumber = String.valueOf(number); // 返回 "123" char[] chars = {'a', 'b', 'c'}; String strChars = String.valueOf(chars); // 返回 "abc" ``` #### `toLowerCase()` 和 `toUpperCase()` 这两个...
### 关于Java中String的一些用法 #### 一、如何比较字符串:`==`与`equals()`的区别 在Java中,比较字符串时通常有两种方法:使用`==`运算符和`equals()`方法。理解这两者的区别对于正确处理字符串至关重要。 1. ...
System.out.println("Today is: " + year + "-" + month + "-" + day); } } ``` 通过以上内容的学习,学习者不仅可以掌握Java常用类库的基本使用方法,还能进一步提高自己的编程能力,为今后更复杂的项目开发打下...
System.out.println("Number is negative or zero."); } ``` 循环: ```java for (int i = 0; i ; i++) { System.out.println(i); } ``` 以及方法调用: ```java public static void printHello() { System.out....
String str = "The number is " + num; ``` 3. 源码解析: Integer.parseInt()和Integer.toString()这两个方法的源码值得深入研究。parseInt()会通过解析字符数组来计算整数值,而toString()则会根据整数值生成...
java回文数源码 实验作业 能用 预览:import java.util.Scanner; public class Ahuiwen { public static void main(String args[]) { System.out.println("输入你要判断的字符串:"+"\n"); Scanner in=...
然而,对于不可变对象,如Java中的String,情况有所不同。尽管String是对象,但一旦创建,其内容就不能改变。在示例代码中: ```java public void simpleChangeString(String original) { original = original + ...
System.out.println("Number is: " + num); } } ``` - **对象创建**:使用关键字 `new` 创建类的对象,并通过调用构造方法初始化对象。 ```java ClassName obj = new ClassName(); ``` - **成员变量**:类...
String inputString = "Hello, my age is 25 and phone number is 123456789."; String regex = "\\d+"; // 正则表达式,表示一个或多个连续的数字 Pattern pattern = Pattern.compile(regex); // 编译正则...
public Student(String className, String name, int studentNumber, int age) { this.className = className; this.name = name; this.studentNumber = studentNumber; this.age = age; } public String ...
Find an efficient algorithm that examines a string of these symbols, say bbbbac, and decides whether or not it is possible to parenthesize the string in such a way that the value of the resulting ...
System.out.println("Is the ID card number valid? " + isValid); } public static boolean validateIDCard(String idCard) { String regex = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$"; Pattern pattern ...
String phoneNumber = "+8613800138000"; String message = "您的验证码为123456"; // 调用第三方API发送短信 sendSms(phoneNumber, message); ``` #### 16. 策略模式 策略模式允许算法独立于使用它的客户端。在...
14 Two Sum II Input array is sorted 49 15 Two Sum III Data structure design 51 16 3Sum 53 17 4Sum 55 18 3Sum Closest 57 19 String to Integer (atoi) 59 20 Merge Sorted Array 61 ... ... 231 Counting ...
System.out.println("The random positive number is: " + number); } } } ``` 2. **找出两个随机整数中的较小值** ```java import java.util.Random; public class SmallerOfTwoNumbers { public static...
System.out.println("Number is positive."); } else if (num ) { System.out.println("Number is negative."); } else { System.out.println("Number is zero."); } for (int i = 1; i ; i++) { System.out....