java取出字符串前面数值的方法

public class Test {

	public static int getIntValue(String str){
		if(str==null){
			return 0;
		}
		String valueSection=str.split("[a-zA-Z]")[0].trim();
		if(valueSection.length()==0)
		{
			return 0;
		}
		int value=0;
		try{
			value=Integer.parseInt(valueSection);
		}catch(Exception e){
			value=0;
		}
		return value;
	}
	
	public static void main(String[] args){
		String aaa="20px";
		int a=Test.getIntValue(aaa);
		System.out.println(a);
	}
}


输出结果为: 20

猜你喜欢

转载自blog.csdn.net/zgrjkflmkyc/article/details/70231770
今日推荐