Is there a faster method to match an arbitrary String to month name in Java

Posted by jonc on Stack Overflow See other posts from Stack Overflow or by jonc
Published on 2010-05-21T03:44:18Z Indexed on 2010/05/21 3:50 UTC
Read the original article Hit count: 168

Filed under:
|
|

Hello, I want to determine if a string is the name of a month and I want to do it relatively quickly. The function that is currently stuck in my brain is something like:

boolean isaMonth( String str ) {
    String[] months = DateFormatSymbols.getInstance().getMonths();
    String[] shortMonths = DateFormatSymbols.getInstance().getShortMonths();
    int i;

    for( i = 0; i<months.length(); ++i;) {
        if( months[i].equals(str) ) return true;
        if( shortMonths[i].equals(str ) return true;
    }
    return false;
}

However, I will be processing lots of text, passed one string at a time to this function, and most of the time I will be getting the worst case of going through the entire loop and returning false.

I saw another question that talked about a Regex to match a month name and a year which could be adapted for this situation. Would the Regex be faster? Is there any other solution that might be faster?

© Stack Overflow or respective owner

Related posts about java

Related posts about string-matching