你如何比较 Java 中的两个版本字符串? [英] How do you compare two version Strings in Java?

查看:29
本文介绍了你如何比较 Java 中的两个版本字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于比较版本号的标准习惯用法?我不能只使用直接的 String compareTo,因为我还不知道最大发布点数是多少.我需要比较版本,并且以下内容成立:

Is there a standard idiom for comparing version numbers? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be. I need to compare the versions and have the following hold true:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

推荐答案

以点为分隔符对字符串进行分词,然后从左边开始并排比较整数翻译.

Tokenize the strings with the dot as delimiter and then compare the integer translation side by side, beginning from the left.

这篇关于你如何比较 Java 中的两个版本字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆