import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestString7 {
public static void main(String args[]){
//java算法----判断字符串是否为数值型字符串
System.out.println(IsNumber1("+1001"));
System.out.println(IsNumber2("+1001"));
}
/**
* 使用字符串的matches方法
* @param s
* @return
*/
public static int IsNumber1(String s){
String regex = "^[1-9][0-9]*\\.[0-9]+$|^[1-9][0-9]*$|^0+\\.[0-9]+$";
char c = s.charAt(0);
boolean bool;
if(c=='+'|c=='-'){
bool = s.substring(1).matches(regex);
}else{
bool=s.matches(regex);
}
if(bool){
return 1;
}else{
return 0;
}
}
/**
* 使用Pattern和Matcher类的方法
* @param s
* @return
*/
public static int IsNumber2(String s){
String regex = "^[1-9][0-9]*\\.[0-9]+$|^[1-9][0-9]*$|^0+\\.[0-9]+$";
Pattern patter = Pattern.compile(regex);
char c = s.charAt(0);
if(c=='+'|c=='-'){
s = s.substring(1);
}
Matcher matcher = patter.matcher(s);
boolean bool = matcher.matches();
if(bool){
return 1;
}else{
return 0;
}
}
}
import java.util.regex.Pattern;
public class TestString7 {
public static void main(String args[]){
//java算法----判断字符串是否为数值型字符串
System.out.println(IsNumber1("+1001"));
System.out.println(IsNumber2("+1001"));
}
/**
* 使用字符串的matches方法
* @param s
* @return
*/
public static int IsNumber1(String s){
String regex = "^[1-9][0-9]*\\.[0-9]+$|^[1-9][0-9]*$|^0+\\.[0-9]+$";
char c = s.charAt(0);
boolean bool;
if(c=='+'|c=='-'){
bool = s.substring(1).matches(regex);
}else{
bool=s.matches(regex);
}
if(bool){
return 1;
}else{
return 0;
}
}
/**
* 使用Pattern和Matcher类的方法
* @param s
* @return
*/
public static int IsNumber2(String s){
String regex = "^[1-9][0-9]*\\.[0-9]+$|^[1-9][0-9]*$|^0+\\.[0-9]+$";
Pattern patter = Pattern.compile(regex);
char c = s.charAt(0);
if(c=='+'|c=='-'){
s = s.substring(1);
}
Matcher matcher = patter.matcher(s);
boolean bool = matcher.matches();
if(bool){
return 1;
}else{
return 0;
}
}
}