码迷,mamicode.com
首页 > 其他好文 > 详细

自己主动更新 -- 版本比較(2)

时间:2017-07-13 10:19:43      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:file   pop   填充   eal   字符   长度   str1   rac   小数点   

版本比較

在实现自己主动更新的时候,须要进行版本的比較。

比如: 1.0.6 和1.0.7比較大小

解决方式:

将版本的字符串转换成整数来比較。

步骤:

1. 去除字符串中的小数点。(使用正則表達式)

2. 比較两个字符串的长度。长度短的后尾补0。直到两个字符串长度同样。

(为了实现 1.0.5.1与1.0.6的比較)。

3. 将两个字符串转换成整数比較。

代码例如以下:

// 正則表達式去除小数点
		//String str1 = "1.0.6".replaceAll("[.]", ""); 
		//String str2 = "1.0.7".replaceAll("[.]", "");
		String str1 = "1.0.5.1".replaceAll("[.]", ""); 
		String str2 = "1.0.6".replaceAll("[.]", "");
		
		//长度不同尾部加0填充
		if(str1.length()<str2.length()){
			for(int i = 0;i< str2.length() - str1.length();i++){
				str1 += "0";
			}
		}else{
			for(int i = 0;i< str1.length() - str2.length();i++){
				str2 += "0";
			}
		}
		
		int i1 = Integer.valueOf(str1);
		int i2 = Integer.valueOf(str2);
		
		System.out.println("str1 => i1 = "+i1);
		System.out.println("str2 => i2 = "+i2);


 

 

 

自己主动更新 -- 版本比較(2)

标签:file   pop   填充   eal   字符   长度   str1   rac   小数点   

原文地址:http://www.cnblogs.com/gccbuaa/p/7158544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!