① java 怎麼判斷一個字元串中是否包含數字
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");
http://jakarta.apache.org/commons/lang/api-release/index.html下面的解釋:
isNumeric
public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
null will return false. An empty String ("") will return true.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = true
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false
Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null
上面三種方式中,第二種方式比較靈活。
第一、三種方式只能校驗不含負號「-」的數字,即輸入一個負數-199,輸出結果將是false;
而第二方式則可以通過修改正則表達式實現校驗負數,將正則表達式修改為「^-?[0-9]+」即可,修改為「-?[0-9]+.?[0-9]+」即可匹配所有數字。
② java中怎麼判斷一個字元串中包含某個字元或字元串
Java中字元串中子串的查找共有四種方法,如下:
1、int indexOf(String str) :返回第一次出現的指定子字元串在此字元串中的索引。
2、int indexOf(String str, int startIndex):從指定的索引處開始,返回第一次出現的指定子字元串在此字元串中的索引。
3、int lastIndexOf(String str) :返回在此字元串中最右邊出現的指定子字元串的索引。
4、int lastIndexOf(String str, int startIndex) :從指定的索引處開始向後搜索,返回在此字元串中最後一次出現的指定子字元串的索引。
示例
下面的示例說明了 indexOf 方法的用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}
public class FirstDemo {
/**
*API中String的常用方法
*/
// 查找指定字元串是否存在
public static void main(String[] args) {
String str1 = "abcdefghijklmnabc";
// 從頭開始查找是否存在指定的字元
System.out.println(str1.indexOf("c"));
// 從第四個字元位置開始往後繼續查找
System.out.println(str1.indexOf("c", 3));
//若指定字元串中沒有該字元則系統返回-1
System.out.println(str1.indexOf("x"));
}
③ java 判斷字元串是否相等
==咯,值相等
===咯,值相等,屬性相同咯
④ java校驗字元串表達式
這個好像沒有什麼好方法.如果你在傳數據的時候事先得到各個比較的結果然後在傳過來會好辦一些.比如1 = 2 AND 2 = 2 OR 3 < 5
你傳為03142,(0假1真3並4或)
⑤ java驗證字元串是否是數字
1、int a = Integer.parseInt("123"); 把字元串123轉換成一個整型,其實也不是直接轉換成整形,是轉換成int的包裝類型,但是int的包裝類型(Integer)可以自動的轉換成int類型。
2、除了這個,還可以把字元串轉換成字元,浮點型,Double.parsetDouble("123.5"),你可以在java編輯器里輸入Double.會出提示。
3、int的包裝類型是Integer ,float,double他們的包裝類型都是首字母大寫,char的包裝類型是Character。
⑥ java怎麼驗證一個字元串的格式
字元串的 public boolean matches(Stringregex) 方法傳遞一個用來匹配此字元串的正則表達式,告知此字元串是否匹配給定的正則表達式。當且僅當此字元串匹配給定的正則表達式時,返回 true,表示匹配。
⑦ java :從控制台輸入一個字元串,驗證是不是郵箱
自定了一個合法郵箱規則,希望能幫助理解。代碼如下:import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個郵箱地址:");String mail = sc.nextLine();/* 設定郵箱地址的合法規則,合法郵箱地址要求如下: (1)字元必須是英文或數字開始 (2)必須包含一個@ (3)@符號在. 符號前面 (4)以英文或數字結尾 */ //設置一個正則表達式 String reg = "[\w]+@[\w]+.[\w]+"; //告知此字元串是否匹配給定的正則表達式。if(mail.matches(reg)) {System.out.println("郵箱地址合法!");}else {System.out.println("郵箱地址不合法!");}}}這里主要是採用正則表達式的方式。
關於正則表達式,查看Pattern類和Matcher類。樓主可以可以到網上查看下相關資料。解釋下上面的正則表達式String reg = "[\w]+@[\w]+.[\w]+";w 表示單詞字元:[a-zA-Z_0-9],上面是兩個反斜桿是因為反斜桿是轉義字元 +號表示:出現一次或多次 ,所以[\w]+意思就是一到多個單詞字元(英文或數字)@ :直接表示@字元.:表示點字元綜上所述。String reg = "[\w]+@[\w]+.[\w]+";的意思就是 :一到多個字元 + @ + 一到多個字元 + 點 + 一到多個字元。正則表達式使用的好。
合法E-mail地址: 1. 必須包含一個並且只有一個符號「@」 2. 第一個字元不得是「@」或者「.」 3. 不允許出現「@.」或者.@ 4. 結尾不得是字元「@」或者「.」 5. 允許「@」前的字元中出現「+」 6. 不允許「+」在最前面,或者「+@」
⑧ java中驗證字元串是不是數字的四種方法
判斷字元串是不是數字,大家可能會用一些java自帶的方法,也有可能用其他怪異的招式,比如判斷是不是整型數字,將字元串強制轉換成整型,不是數字的就會拋出錯誤,那麼就不是整型的了。但本文介紹的比較好的兩種方法:
1。java類庫自帶的方法:
public boolean isNum(String msg){
if(java.lang.Character.isDigit(msg.charAt(0))){
return true;}return false;}0202更新:發現以上方法寫得不夠到位,現在就改為下面的簡單說明了,至於具體的方法實現字元串判斷是否數字就不寫了。
java.lang.Character.isDigit(char ch) boolean
isDigit 只能作用於char,所以判斷字元串是否為數字,要一個一個拿出char進行判斷。
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;}02
3。用正則表達式