热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php正则判断字符串后面的数字,使用正则判断字符串是否能转为数字

这次给大家带来使用正则判断字符串是否能转为数字,使用正则判断字符串是否能转为数字的注意事项有哪些,下面就是实战案例,一起来看一下。代码如下

这次给大家带来使用正则判断字符串是否能转为数字,使用正则判断字符串是否能转为数字的注意事项有哪些,下面就是实战案例,一起来看一下。代码如下所示:package java_test;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* @author: gznc_pcc

* @date:2018年6月1日 10:50:38

* @version :

*

*/

class Main

{

public static void main(String[] args) {

String lineString = "[\"1\"]";

String line = "[\"on\",\"1\",\"5\",\"8\",\"10\"]";

lineString = line.replaceAll("[\"\\[\\]]", "");//用""替换" [ ]

String[] word = lineString.split(","); //以,切割

System.out.println(lineString);

for(int i=0;i

Pattern pattern = Pattern.compile("[0-9]*"); //正则,匹配数字

Matcher matcher = pattern.matcher(word[i]);

if(matcher.matches()){

System.out.println("1:可以转换");

System.out.println(Integer.parseInt(word[i]));

}

else {

System.out.println("2:不能转换");

System.out.println(word[i]);

}

}

}

}

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:



推荐阅读
author-avatar
棒棒糖的困惑
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有