1. 程式人生 > >java中判斷字串是否為數字的方法 StringUtil包函式

java中判斷字串是否為數字的方法 StringUtil包函式

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判斷字串是否數字的五種方法

//方法一:用JAVA自帶的函式 public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ if (!Character.isDigit(st

java判斷字串是否數字方法 StringUtil函式

java中判斷字串是否為數字的方法: 1.用JAVA自帶的函式 public static boolean isNumeric(String str){   for (int i = 0; i < str.length(); i++){    System.out.p

java判斷字串是否數字的三種方法

1用JAVA自帶的函式 public static boolean isNumber(String str){ boolean re = true; for(int i=0;i<str.length();i++){ if(!Character.isDigit(str

Java判斷字串是否數字的幾種方法

原文地址:http://blog.csdn.net/u013066244/article/details/53197756 用JAVA自帶的函式 public static boolean isNumericZidai(String str) {

java判斷字串是否數字方法的幾種方法

java中判斷字串是否為數字的方法: 1.用JAVA自帶的函式 public static boolean isNumeric(String str){ for (int i = 0; i < str.length(); i++){ System.out.

java判斷字串是否數字或小數

public static boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); if(str.indexOf(".")>0){//判斷是否有小數點 if(str.

java 判斷字串是否數字(包含負數)

public static void main(String[] args){ System.out.println(AssistController.isNumeric("-77"));}public static boolean isNumeric(String str){ Pattern pa

Java判斷物件是否空的方法

首先來看一下工具StringUtils的判斷方法: 一種是org.apache.commons.lang3包下的; 另一種是org.springframework.util包下的。這兩種StringUtils工具類判斷物件是否為空是有差距的: Str

java判斷字串是否數字或中文或字母

來自:晞瀾 1.判斷字串是否僅為數字: 1>用JAVA自帶的函式 public static boolean isNumeric(String str){   for (int i = str.length();--i>=0;){       if (!Ch

Java判斷字串是否數字

package com.java.demo; import java.util.regex.Matcher; import java.util.regex.Pattern; public clas

java判斷字串真實長度(中文2個字元,英文1個字元)的方法

public class Char_cn { public static void main(String[] args) { // TODO Auto-generated method stub String haha = "呵呵呵呵abcd";

js判斷字串是否某種數字

函式 isNaN(val)//如果是數字則返回 false 使用時候 if(!isNaN(val)){   alert("是數字");}else{   alert("不是數字");} 判斷: <script> document.write(isNaN

java判斷字串是否含有中文、數字、字母

java中判斷某一字串是否為純英文、純數字、字串中含有英文和數字,判斷字串是否為純中文,我們通過正則str.matches匹配,告訴這個字串是否與給定的正則表示式匹配。對string .matches(regex)方法的呼叫會產生與表示式完全相同的結果        /**

java判斷字串是否數字、字母、漢字

public class ZhStringUtil { /** * 判斷字串是否為數字 * @param str * @return */ pu

java判斷字串是否包含數字

在javascript中有一個方法isDigit()使用來判斷一個字串是否都是數字,在java的字串處理方法中沒有這樣的方法,昨天在介面中用到了,順便查了下,總結了下,下次用到,順手拈來// 判斷一個字

Android工具類,一些方法電話正則,是否空,日期格式化,星期,車牌,身份證校驗,設定地區編碼,驗證日期字串是否是YYYY-MM-DD格式,判斷字串是否數字

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Gr

JAVA判斷字串是否數字或者是否以0開頭

public static boolean isNumeric(String str){ if(str.matches("\\d*"){ return true; }else{ return false; } }4.用ascii碼 public static boolean isNumeric(Strin

java判斷字串漢字的個數

使用方法3與方法4時需要注意一些問題,程式碼中會有註釋。 程式碼如下: package 包名; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; publ

java判斷字串郵箱的個數

使用到:郵箱的正則表示式,列表的部分知識,Matcher方法 package 包名; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public cla

js 判斷字串是否數字(正整數)

/** * 判斷字串是否為數字 * @param nubmer * @returns {boolean} */ function checkRate(nubmer) { //判斷正整數/[1−9]+[0−9]∗]∗/ var re = /^[0-9]+.?[0-9]*/;