java中判断字符串是否为数字的方法:

1.用JAVA自带的函数
public static boolean isNumeric(String str){
  for (int i = 0; i < str.length(); i++){
   System.out.println(str.charAt(i));
   if (!Character.isDigit(str.charAt(i))){
    return false;
   }
  }
  return true;
 }

2.用正则表达式
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
public boolean isNumeric(String str){
   Pattern pattern = Pattern.compile("[0-9]*");
   Matcher isNum = pattern.matcher(str);
   if( !isNum.matches() ){
       return false;
   }
   return true;
}

3.使用org.apache.commons.lang
org.apache.commons.lang.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");

上面三种方式中,第二种方式比较灵活。
 
第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;

而第二方式则可以通过修改正则表达式实现校验负数,将正则表达式修改为“^-?[0-9]+”即可,修改为“-?[0-9]+.?[0-9]+”即可匹配所有数字。

这里主要介绍

StringUtil包函数

org.apache.commons.lang.StringUtil(StringUtil包函数(用法))

1.空字符串检查
使用函数: StringUtils.isBlank(testString)
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False 例程:
String test = "";
String test2 = "\n\n\t";
String test3 = null;
String test4 = "Test";
System.out.println( "test blank? " + StringUtils.isBlank( test ) );
System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );
System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );
System.out.println( "test4 blank? " + StringUtils.isBlank( test4 ) );
输出如下:
test blank? true
test2 blank? true
test3 blank? true
test4 blank? False
函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.

2.清除空白字符
使用函数: StringUtils.trimToNull(testString)
函数介绍:清除掉testString首尾的空白字符,如果仅testString全由空白字符(whitespace)组成则返回null 例程:
String test1 = "\t";
String test2 = "  
A  Test  ";
String test3 = null;
System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) );
System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) );
System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );
输出如下:
test1 trimToNull: null
test2 trimToNull: A  
Test
test3 trimToNull: null
注意:函数StringUtils.trim(testString)与StringUtils.trimToNull(testString)功能类似,但testString由空白字符(whitespace)组成时返回零长度字符串。

3.取得字符串的缩写
使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串. 例程:
String test = "This is a test of the abbreviation.";
String test2 = "Test";
System.out.println( StringUtils.abbreviate( test, 15 ) );
System.out.println( StringUtils.abbreviate( test, 5,15 ) );
System.out.println( StringUtils.abbreviate( test2, 10 ) );
输出如下:
This is a te...
...is a test...
Test
4.劈分字符串
使用函数: StringUtils.split(testString,splitChars,arrayLength)
函数介绍:splitChars中可以包含一系列的字符串来劈分testString,并可以设定得到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下不要设定长度.
例程:
String input = "A b,c.d|e";
String input2 = "Pharmacy, basketball funky";
String[] array1 = StringUtils.split( input, " ,.|");
String[] array2 = StringUtils.split( input2, " ,", 2 );
System.out.println( ArrayUtils.toString( array1 ) );
System.out.println( ArrayUtils.toString( array2 ) );
输出如下:
{A,b,c,d,e}
{Pharmacy,basketball funky}

5.查找嵌套字符串
使用函数:StringUtils.substringBetween(testString,header,tail)
函数介绍:在testString中取得header和tail之间的字符串。不存在则返回空
例程:
String htmlContent = "ABC1234ABC4567";
System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));
System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));
输出如下:
ABC
null

6.去除尾部换行符
使用函数:StringUtils.chomp(testString)
函数介绍:去除testString尾部的换行符
例程:
String input = "Hello\n";
System.out.println( StringUtils.chomp( input ));
String input2 = "Another test\r\n";
System.out.println( StringUtils.chomp( input2 ));
输出如下:
Hello
Another test

7.重复字符串
使用函数:StringUtils.repeat(repeatString,count)
函数介绍:得到将repeatString重复count次后的字符串
例程:
System.out.println( StringUtils.repeat( "*", 10));
System.out.println( StringUtils.repeat( "China ", 5));
输出如下:
**********
China China China China China

其他函数:StringUtils.center( testString, count,repeatString );
函数介绍:把testString插入将repeatString重复多次后的字符串中间,得到字符串的总长为count
例程:
System.out.println( StringUtils.center( "China", 11,"*"));
输出如下:
***China***

8.颠倒字符串
使用函数:StringUtils.reverse(testString)
函数介绍:得到testString中字符颠倒后的字符串
例程:
System.out.println( StringUtils.reverse("ABCDE"));
输出如下:
EDCBA

9.判断字符串内容的类型
函数介绍:
StringUtils.isNumeric( testString ) :如果testString全由数字组成返回
True StringUtils.isAlpha( testString ) :如果testString全由字母组成返回
True StringUtils.isAlphanumeric( testString ) :如果testString全由数字或数字组成返回
True StringUtils.isAlphaspace( testString )  :如果testString全由字母或空格组成返回True
例程:
String state = "Virginia";
System.out.println( "Is state number? " + StringUtils.isNumeric(state ) );
System.out.println( "Is state alpha? " + StringUtils.isAlpha( state ));
System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );
System.out.println( "Is state alphaspace? " + StringUtils.isAlphaSpace( state ) );
输出如下:
Is state number? false
Is state alpha? true
Is state alphanumeric? true
Is state alphaspace? true

10.取得某字符串在另一字符串中出现的次数
使用函数:StringUtils.countMatches(testString,seqString)
函数介绍:取得seqString在testString中出现的次数,未发现则返回零
例程:
System.out.println(StringUtils.countMatches( "Chinese People", "e"));
输出:
4

11.部分截取字符串
使用函数:
StringUtils.substringBetween(testString,fromString,toString ):取得两字符之间的字符串
StringUtils.substringAfter( ):取得指定字符串后的字符串
StringUtils.substringBefore( ):取得指定字符串之前的字符串
StringUtils.substringBeforeLast( ):取得最后一个指定字符串之前的字符串
StringUtils.substringAfterLast( ):取得最后一个指定字符串之后的字符串
函数介绍:上面应该都讲明白了吧。
例程:
String formatted = " 25 * (30,40) [50,60] | 30";
System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );
System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(", "," ) );
System.out.print(", N2: " + StringUtils.substringBetween( formatted, ",", ")" ) );
System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[", "," ) );
System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",", "]" ) );
System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) );
输出如下:
N0:  
25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5:  
30

java中判断字符串是否为数字的方法 StringUtil包函数相关推荐

  1. java中判断字符串是否为数字(正整数)

    标题:java中判断字符串是否为数字(正整数) 遍历s的每一个字符 private boolean isNumeric(String s) {for (int i = 0; i < s.leng ...

  2. Java中判断字符串是否为数字的五种方法

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 推荐使用第二个方法,速度最快. 方法一:用JAVA自带的函数 Java代码   public sta ...

  3. java中判断字符串是否为数字的三种方法

    1用JAVA自带的函数 public static boolean isNumeric(String str){for (int i = str.length();--i>=0;){ if (! ...

  4. Java中判断字符串是否为数字

    使用org.apache.commons.lang StringUtils.isNumeric(null) = false StringUtils.isNumeric("") = ...

  5. C#中判断字符串是否为数字的方法

    //引用正则表达式空间 using System.Text.RegularExpressions; //具体方法 public bool IsNumeric(string value) {return ...

  6. java 判断是否中文字符_java中判断字符串是否是中文的方法

    java中判断字符串是否是中文的方法 发布时间:2020-06-25 14:07:38 来源:亿速云 阅读:79 作者:Leah 本篇文章为大家展示了java判断字符串是否为中文的方法,代码简明扼要并 ...

  7. Shell中判断字符串是否为数字的6种方法

    Shell中判断字符串是否为数字的6种方法 #!/bin/bash # 方法1 a=1234;echo "$a"|[ -n "`sed -n '/^[0-9][0-9]* ...

  8. java中判断字符串实际长度(中文2个字符,英文1个字符)的方法,包含中英文符号

    java中判断字符串实际长度(中文2个字符,英文1个字符)的方法,包含中英文符号 介绍 数据库中很多字段都会限制长度,为了方便用户使用,在前端界面给出限制长度的提示,看网上很多写的获取字符串长度都没有 ...

  9. Java中判断字符串是否相等

    Java中判断字符串是否相等 相等判断操作符`==` 相等判断方法`equals()` `equals()`方法原理 总结 在最近的开发中,我踩到一个坑,过程是这样的.我需要在Java中判断两个字符串 ...

最新文章

  1. 先序中序数组推后序数组
  2. 架构的“一小步”,业务的一大步 1
  3. 附录:更多字典操作命令
  4. RabbitMq(二) Connection、Channels、Exchanges、Queues 等基本概念介绍
  5. 周立功的linux开发板例程,USB NXP LPC1766 开发配套例程(周立功开发板自带例程) - 下载 - 搜珍网...
  6. 路由表(FIB)的初始化
  7. 【转】HTTP请求中的form data和request payload的区别
  8. java单例模式之深入浅出
  9. Layui 中引入Echarts模块
  10. 计算机桌面死机的原因是,假死机(电脑桌面假死或卡死)
  11. 巴菲特指标:估值过高
  12. 新电脑如何进行磁盘分区?
  13. svn: E230001: Server SSL certificate verification failed: certificate issued for a different hostnam
  14. Netty03-进阶
  15. 国家开放大学《国际经济法》第二章 国际货物买卖法 边学边练
  16. 由 An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution 引起的思考
  17. Windows程式开发设计指南(十五)与装置无关的点阵图
  18. 如何清理占用计算机内存,告诉你如何深度清理电脑内存
  19. Linux学习笔记(九)
  20. pringboot毕业生就业信息管理 毕业设计-附源码151501

热门文章

  1. Charles的功能介绍与使用教程,一学就会,不信就来试试?
  2. NXP的MSCAN模块波特率设置
  3. 随时随地编写运行调试代码(Android)
  4. ArcView GIS 应用与开发技术(4)-地图投影
  5. UE4学习(一)C++编程官方文档解读
  6. 当ChatGPT续写《红楼梦》,能替代原著吗?
  7. Windows查看最近访问的文件目录或文件夹
  8. 如何更改默认鼠标光标
  9. PMP learning
  10. 打印系统开发(15)——WinForm开发(43)——winform 使用打印机