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

  • SEARCH
  • Home
  • 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 9148693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:20:39+00:00 2026-06-17T11:20:39+00:00

Possible Duplicate: Matching a . in java I have a String 1.2.4 which i

  • 0

Possible Duplicate:
Matching a “.” in java

I have a String 1.2.4 which i want to split and get 2 to compare it with another String that i get.

I am trying to do the following

    StringTokenizer old_ver = new StringTokenizer(oldVersion, ".");
    int oldVer = Integer.parseInt(old_ver.nextToken());
    oldVer = Integer.parseInt(old_ver.nextToken());

    StringTokenizer new_ver = new StringTokenizer(newVersion, ".");
    int newVer = Integer.parseInt(new_ver.nextToken());
    newVer = Integer.parseInt(new_ver.nextToken());

    if (oldVer == newVer) {
        return true;
    }

IS there a better way to do it using .Split()

  • 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-06-17T11:20:40+00:00Added an answer on June 17, 2026 at 11:20 am

    In your code, 1.2.1 would be “compatible” with 3.2.0, and this looks like a serious problem, regardless if you use String.split or not. Also, you do not need to parse version numbers into integers as you only compare for equality.

     StringTokenizer old_ver = new StringTokenizer(oldVersion, ".");
     StringTokenizer new_ver = new StringTokenizer(newVersion, ".");
     return (old_ver.nextToken().equals(new_ver.nextToken() && 
             old_ver.nextToken().equals(new_ver.nextToken() );
    

    You can surely do with String.split as well:

       String [] oldV = String.split(oldVersion,"\\.");
       String [] newV = String.split(newVersion,"\\.");
    
       return oldV[0].equals(newV[0]) && oldV[1].equals(newV[1]);
    

    The String.split() version seems slightly shorter but but for me it looks slightly more difficult to read because:

    • It involves additional thinking step that dot (.) is a reserved char and must be escaped.
    • “Next element and then following element” seems involving less thinking effort than “element at position 0 and then element at position 1”.

    It may pay to have some version comparator that first compares major, then intermediate and then minor parts of the version number the way that we could have the tests like “1.2.4 or later”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Regex for matching javadoc fragments I have files having content like /**
Possible Duplicate: Splitting string array based upon digits in php? I have a set
Possible Duplicate: How to escape text for regular expression in Java I have a
Possible Duplicate: Scala: short form of pattern matching that returns Boolean In my scala
Possible Duplicate: Javascript: strip out non-numeric characters from string String matching is headache for
Possible Duplicate: Matching Nested Structures With Regular Expressions in Python I am trying to
Possible Duplicate: scale and rotation Template matching I have a template grayscale image ,
Possible Duplicate: Regular Expression matching for entire string On my form page, I am
Possible Duplicate: Convert std::string to const char* or char* I have a std::string and
Possible Duplicate: typeof for RegExp I have a routine that is testing to see

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.