IAmYourFaja
IAmYourFaja

Reputation: 56934

Java: Efficient way to determine if a String meets several criteria?

I would like to find an efficient way (not scanning the String 10,000 times, or creating lots of intermediary Strings for holding temporary results, or string bashing, etc.) to write a method that accepts a String and determine if it meets the following criteria:

Here's my attempt so far:

private boolean isInProperForm(final String token) {
    if(token.length() < 2)
        return false;

    char firstChar = token.charAt(0);
    String restOfToken = token.substring(1);
    String firstCharAsString = firstChar + "";
    String firstCharStrToUpper = firstCharAsString.toUpperCase();

    // TODO: Giving up because this already seems way too complicated/inefficient.
    // Ignore the '&& true' clause - left it there as a placeholder so it wouldn't  give a compile error.
    if(firstCharStrToUpper.equals(firstCharAsString) && true)
        return true;

    // Presume false if we get here.
    return false;
}

But as you can see I already have 1 char and 3 temp strings, and something just doesn't feel right. There's got to be a better way to write this. It's important because this method is going to get called thousands and thousands of times (for each tokenized word in a text document). So it really really needs to be efficient.

Thanks in advance!

Upvotes: 1

Views: 1237

Answers (7)

Shinto Joseph
Shinto Joseph

Reputation: 1171

return token.matches("[A-Z].[a-z].");

Upvotes: 0

jlordo
jlordo

Reputation: 37823

What about:

return token.matches("[A-Z].*[a-z].*");

This regular expression starts with an uppercase letter and has at least one following lowercase letter and therefore meets your requirements.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200206

This function should cover it. Each char is examined only once and no objects are created.

public static boolean validate(String token) {
  if (token == null || token.length() < 2) return false;
  if (!Character.isUpperCase(token.charAt(0)) return false;
  for (int i = 1; i < token.length(); i++)
    if (Character.isLowerCase(token.charAt(i)) return true;
  return false;

Upvotes: 6

amit
amit

Reputation: 178491

The first criteria is simply the length - this data is cached in the string object and is not requiring traversing the string.

You can use Character.isUpperCase() to determine if the first char is upper case. No need as well to traverse the string.

The last criteria requires a single traversal on the string- and stop when you first find a lower case character.


P.S. An alternative for the 2+3 criteria combined is to use a regex (not more efficient - but more elegant):

return token.matches("[A-Z].*[a-z].*");

The regex is checking if the string starts with an upper case letter, and then followed by any sequence which contains at least one lower case character.

Upvotes: 2

corsiKa
corsiKa

Reputation: 82589

  • It is at least 2 characters in length
  • The first character is uppercased
  • The remaining substring after the first character contains at least 1 lowercased character

Code:

private boolean isInProperForm(final String token) {
    if(token.length() < 2) return false;
    if(!Character.isUpperCase(token.charAt(0)) return false;
    for(int i = 1; i < token.length(); i++) {
        if(Character.isLowerCase(token.charAt(i)) {
            return true; // our last criteria, so we are free 
                         // to return on a met condition
        }
    }
    return false; // didn't meet the last criteria, so we return false
}

If you added more criteria, you'd have to revise the last condition.

Upvotes: 1

Vincenzo Maggio
Vincenzo Maggio

Reputation: 3869

To check if first char is uppercase you can use:

Character.isUpperCase(s.charAt(0))

Upvotes: 0

Tom G
Tom G

Reputation: 3660

To find if the first character is uppercase:

Character.isUpperCase(token.charAt(0))

To check if there is at least one lowercase:

if(Pattern.compile("[a-z]").matcher(token).find()) {
    //At least one lowercase
}

Upvotes: 0

Related Questions