Java判断是数字还是字符串

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

方法一:利用正则表达式

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的话...却无效如果有需要的话,...

get/post方式调用http接口

get/post方式调用http接口

 1. 项目环境如下:myeclipse8.5 、tomcat5.0/weblogic、xp、JDK:开发1.5,编译1.4为了方便,在原来的web项目UpDown中新建了一个httpcal...

freemarker 判断日期变量为空处理 及InvalidReferenceException异常处理

at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98);InvalidR...

jquery 操作html元素(及CSS)

jquery 操作html元素(及CSS)

 $("li").addClass("aui-user-view-cell aui-img") ;  //给元素添加样式$('#a1...

Java 实现用户资料完整度的前端显示(或根据填写资料自动评分)

前端使用 Bootstrap 的进度条组件显示百分比,后台读取权重并计算信息完整度,并将计算的结果返回给前端,供页面显示。CSS1<link href="static/sc/...

MyEclipse 中项目修改SVN的地址

MyEclipse 中项目修改SVN的地址

在工作环境调整时,有的时候SVN服务器的地址需要修改,而正在开发中的项目在Eclipse中有些代码没有提交,此时怎么修改SVN的地址呢?以下有一个简单的办法:一、在MyEclipse中选择Window...

评论列表

发表评论

访客

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