判断字符串是不是数值串boolean is_numeric(String str)

简介:  方法: /** * check a string whether is numeric or not * @param String str * @return boolean true or false */ public static boolean is...

 方法:


	/**
	 * check a string whether is numeric or not 
	 * @param String str 
	 * @return boolean true or false
	 */
	public static boolean is_numeric(String str) {
		try {
			Double d= Double.valueOf(str);
		} catch (java.lang.Exception e) {
			return false;
		}
		return true;
	}


测试代码:


public class Test {

	/**
	 * test the is_numeric method
	 * @param args 
	 */
	public static void main(String[] args) { 
		
		String s = "123.123";
		if (is_numeric(s)) {
			System.out.println(s+" is numeric");
		} else {
			System.out.println(s+" not numeric");
		} 
		
		String a = "123";
		if (is_numeric(a)) {
			System.out.println(a+" is numeric");
		} else {
			System.out.println(a+" not numeric");
		}
		
		String b = "12ad";
		if (is_numeric(b)) {
			System.out.println(b+" is numeric");
		} else {
			System.out.println(b+" not numeric");
		} 
 	}

	/**
	 * check a string whether is numeric or not 
	 * @param String str 
	 * @return boolean true or false
	 */
	public static boolean is_numeric(String str) {
		try {
			Double d= Double.valueOf(str);
		} catch (java.lang.Exception e) {
			return false;
		}
		return true;
	}
}

测试输出:


123.123 is numeric
123 is numeric
12ad not numeric


相关文章
|
15天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
201 100
|
15天前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
242 99
|
19天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
19天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
2月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
218 92
|
3月前
|
自然语言处理 Java Apache
在Java中将String字符串转换为算术表达式并计算
具体的实现逻辑需要填写在 `Tokenizer`和 `ExpressionParser`类中,这里只提供了大概的框架。在实际实现时 `Tokenizer`应该提供分词逻辑,把输入的字符串转换成Token序列。而 `ExpressionParser`应当通过递归下降的方式依次解析
235 14
|
SQL 流计算 OceanBase
OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
【2月更文挑战第25天】OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
275 3
|
前端开发 JavaScript
前端基础 - JavaScript值Boolean类型的默认转换
前端基础 - JavaScript值Boolean类型的默认转换
117 0
|
Python
python布尔类型 (Boolean Type)
【8月更文挑战第3天】
373 8