Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6140443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:07:08+00:00 2026-05-23T18:07:08+00:00

Possible Duplicate: How do you compare two version Strings in Java? I’ve 2 strings

  • 0

Possible Duplicate:
How do you compare two version Strings in Java?

I’ve 2 strings which contains version information as shown below:

str1 = "1.2"
str2 = "1.1.2"

Now, can any one tell me the efficient way to compare these versions inside strings in Java & return 0 , if they’re equal, -1, if str1 < str2 & 1 if str1>str2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:07:08+00:00Added an answer on May 23, 2026 at 6:07 pm

    Requires commons-lang3-3.8.1.jar for string operations.

    /**
     * Compares two version strings. 
     * 
     * Use this instead of String.compareTo() for a non-lexicographical 
     * comparison that works for version strings. e.g. "1.10".compareTo("1.6").
     * 
     * @param v1 a string of alpha numerals separated by decimal points. 
     * @param v2 a string of alpha numerals separated by decimal points.
     * @return The result is 1 if v1 is greater than v2. 
     *         The result is 2 if v2 is greater than v1. 
     *         The result is -1 if the version format is unrecognized. 
     *         The result is zero if the strings are equal.
     */
    
    public int VersionCompare(String v1,String v2)
    {
        int v1Len=StringUtils.countMatches(v1,".");
        int v2Len=StringUtils.countMatches(v2,".");
    
        if(v1Len!=v2Len)
        {
            int count=Math.abs(v1Len-v2Len);
            if(v1Len>v2Len)
                for(int i=1;i<=count;i++)
                    v2+=".0";
            else
                for(int i=1;i<=count;i++)
                    v1+=".0";
        }
    
        if(v1.equals(v2))
            return 0;
    
        String[] v1Str=StringUtils.split(v1, ".");
        String[] v2Str=StringUtils.split(v2, ".");
        for(int i=0;i<v1Str.length;i++)
        {
            String str1="",str2="";
            for (char c : v1Str[i].toCharArray()) {
                if(Character.isLetter(c))
                {
                    int u=c-'a'+1;
                    if(u<10)
                        str1+=String.valueOf("0"+u);
                    else
                        str1+=String.valueOf(u);
                }
                else
                    str1+=String.valueOf(c);
            }            
            for (char c : v2Str[i].toCharArray()) {
                if(Character.isLetter(c))
                {
                    int u=c-'a'+1;
                    if(u<10)
                        str2+=String.valueOf("0"+u);
                    else
                        str2+=String.valueOf(u);
                }
                else
                    str2+=String.valueOf(c);
            }
            v1Str[i]="1"+str1;
            v2Str[i]="1"+str2;
    
                int num1=Integer.parseInt(v1Str[i]);
                int num2=Integer.parseInt(v2Str[i]);
    
                if(num1!=num2)
                {
                    if(num1>num2)
                        return 1;
                    else
                        return 2;
                }
        }
        return -1;
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I compare strings in Java? String s1 = andrei; String
Possible Duplicate: How do I compare strings in Java? why first comparison ( s1
Possible Duplicate: how to compare case insensitive two strings in php I need to
Possible Duplicate: javascript compare strings without being case sensitive. I am having two fields.
Possible Duplicate: How to compare strings? ( == returns wrong value) I have the
Possible Duplicate: What is the best way to compare two entity framework entities? I
Possible Duplicate: How can I compare two dates, return a number of days I
Possible Duplicate: How to compare two word documents? How can you get the diff
Possible Duplicate: How do I compare a raw time in Java? For example suppose
Possible Duplicate: How to compare two dates in Objective-C I'd like to pop up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.