Java判断是数字还是字符串

1549年前 (2017-03-06)java技术4810

方法一:利用正则表达式

public class Testone {
public static void main(String[] args){
String str="123456";
boolean result=str.matches("[0-9]+");
if (result == true) {
System.out.println("该字符串是纯数字");}else{System.out.println("该字符串不是纯数字");}}}

方法二:利用Pattern.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Testone {
public static void main(String[] args){
String str="123456";
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher((CharSequence)str);
boolean result=matcher.matches();
System.out.println("该字符串是纯数字");}else{System.out.println("该字符串不是纯数字");}}}


本文原创,转载必追究版权。

分享给朋友:

相关文章

showModalDialog取得父窗口的方法

 通常使用window.open的方式开启新窗口的话要取得父窗口的控件,可以用window.opener来取得父窗口然而如果使用showModalDialog的话...却无效如果有需要的话,...

MyEclipse 报错:'Building workspace' has encountered a problem解决方法

MyEclipse 报错:'Building workspace' has encountered a problem解决方法

          每次MyEclipse运行 工作空间报错如下:'Building workspac...

jfinal 定时任务

1、去quartz官网下载 定时任务jar包(http://www.quartz-scheduler.org/)quartz-*.*.*.jar2、创建定时任务:public class SetAre...

如果总是有人说你瘦,如何神回复

如果总是有人说你瘦,如何神回复

恩,我不做领导很多年了邓小平爷爷 也很瘦哦, 并不影响成为一个伟大的男人我这是高寿(瘦)别说我瘦,我只是胖的不明显而已!我就是瘦着玩玩,谁像你胖得那么认真。嗳,你还别说,你胖了! 恩 ,真的!真的吗?...

UUID 生成方法

import java.util.UUID;/**     * UUID 生成方法     * @return uuid &nbs...

MySQL、Oracle数据库字段分别设置默认系统时间

MySQL设置方法:1、将字段类型设为  TIMESTAMP 2、将默认值设为  CURRENT_TIMESTAMPMySQL 脚本实现用例--添加CreateTime 设置默认时...

评论列表

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。