java中判断字符串是否为数字的两种方法
2007-03-02 16:55
1031 查看
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用正则表达式
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
例子:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class tel...{
public boolean isNumeric(String str)...{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )...{
return false;
}
return true;
}
private String toNo(String telno)...{
String temp="";
int j=0;
if(isNumeric(telno))
...{
return telno;
}
else...{
for(int i=0;i<telno.length();i++)...{
if(!(Character.isDigit(telno.charAt(i))))...{
continue;
}
else
...{
temp += telno.charAt(i);
}
}
}
return temp;
}
public static void main(String[] args)...{
tel test = new tel();
String no="0592-3924063";
String no2="(0592)3924063";
String no3="05923924063";
System.out.println(test.toNo(no));
System.out.println(test.toNo(no2));
System.out.println(test.toNo(no3));
}
}
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用正则表达式
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
例子:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class tel...{
public boolean isNumeric(String str)...{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )...{
return false;
}
return true;
}
private String toNo(String telno)...{
String temp="";
int j=0;
if(isNumeric(telno))
...{
return telno;
}
else...{
for(int i=0;i<telno.length();i++)...{
if(!(Character.isDigit(telno.charAt(i))))...{
continue;
}
else
...{
temp += telno.charAt(i);
}
}
}
return temp;
}
public static void main(String[] args)...{
tel test = new tel();
String no="0592-3924063";
String no2="(0592)3924063";
String no3="05923924063";
System.out.println(test.toNo(no));
System.out.println(test.toNo(no2));
System.out.println(test.toNo(no3));
}
}
相关文章推荐
- java中判断字符串是否数字的两种方法
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- java中判断字符串是否数字的两种方法
- java中判断字符串是否数字的两种方法
- java中判断字符串是否为数字的方法的几种方法
- java中判断字符串是否为数字的方法
- 在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现。 【方法一】:使用 try{} catch{} 语句。 我们可以在try语句块中试图
- Java中判断字符串是否为数字的三种方法
- java中判断字符串是否为数字的三种方法
- java中判断字符串是否为数字的三种方法
- Java中判断字符串是否为数字的五种方法
- 字符串--java中判断字符串是否为数字的方法的几种方法?
- java中判断字符串是否为数字的三种方法
- java中判断字符串是否为数字的三种方法
- java中判断字符串是否为数字的方法的几种方法
- Java中判断字符串是否为数字的五种方法 (转)
- java中判断字符串是否为数字的三种方法
- Java中判断字符串是否为数字的五种方法
- java中判断字符串是否为数字的三种方法
- 判断一个数是否是回文数(两种方法1.将数字翻转看与原来是否相等2.当作字符串处理看是不是左右对称相等)