SDJ
SDJ

Reputation: 43

Java : startsWith() with different prefix and not only one

I need to write some code to check the length of my input AND the beginning of it. Depending on those two parameters, I'll have to modify my input. I succeeded in checking the length of my input. But I struggle when I try to check the beginning of my input.

Here I am so far :

Long number = ean12;
String inputEan12 = Long.toString(ean12);

if (inputEan12.length() == 12) {
    if (inputEan12.startsWith("02"||"21"||"22"||"23"||"24"||"25"||"26"||"27"||"28"||"29")
        //SOME CODE 
    } else {
        //SOME OTHER CODE
    }

} else if (inputEan12.length() == 11) {
    //SOME MORE CODE

} else {
    System.out.println("All is good!");
}

I chose to use the ".startsWith()" method to do my check, thinking it would solve my problem easily. But I can only do one check at a time. It seems that I would need to add an "if" for each beginning/number I want to check. It would turn into a big white elephant...

So I thought of using a table (for example : "String []myTable") to put inside all of my "beginning variables" and to loop on that table. But this startsWith() method doesn't allow me to do that. It seems like my prefix has to be simple.

Any solution or advice to give me to check the beginning of my input easily?

Upvotes: 0

Views: 1568

Answers (6)

Paul
Paul

Reputation: 20081

If you're willing to use an external library then Commons Lang StringUtils.startsWithAny will work:

if (StringUtils.startsWithAny(inputEan12, "02", "21", "22", etc...)
{
    // do stuff
}

Upvotes: 1

Ahmed Ashour
Ahmed Ashour

Reputation: 5549

Or simply iterate:

public static void main(final String[] args) {
    String inputEan12 = "ean12";

    if (inputEan12.length() == 12) {
        if (startsWith(inputEan12, "02", "21", "22", "23", "24", "25", "26", "27", "28", "29")) {
            //SOME CODE
        } else {
            //SOME OTHER CODE
        }
    }
}

private static boolean startsWith(final String string, final String... possibilities) {
    for (String prefix : possibilities) {
        if (string.startsWith(prefix)) {
            return true;
        }
    }
    return false;
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201477

There is no or short syntax like that with startsWith. You could use multiple calls to String.startsWith seperated by || (which is logically what you have written). But, it would be shorter to take an array of valid prefixes and the substring of inputEan12 you care about and use anyMatch with Arrays.stream. Like,

Long number = ean12;
String inputEan12 = Long.toString(ean12);
String[] prefixes = { "02", "21", "22", "23", "24", "25", "26", "27", "28", "29" };
if (inputEan12.length() == 12) {
    String firstTwo = inputEan12.substring(0, 2);
    if (Arrays.stream(prefixes).anyMatch(firstTwo::equals)) {
        // SOME CODE
    } else {
        // SOME OTHER CODE
    }
} else if (inputEan12.length() == 11) {
    // SOME MORE CODE

} else {
    System.out.println("All is good!");
}

Upvotes: 0

HBo
HBo

Reputation: 633

Here it is, hope it's what you're looking for:

List<String> possibleValues = Arrays.asList("02", "21", "22", "23", "24", "25", "26", "27", "28", "29");
if (possibleValues.contains(inputEan12.substring(0, 2))) {
         //SOME CODE 
}

Upvotes: 2

Elarbi Mohamed Aymen
Elarbi Mohamed Aymen

Reputation: 1710

you can use .matches(String regex) like this :

if (inputEan12.matches("^(02|21|22|23|24|25|26|27|28|29).*$"){ }

Upvotes: 1

daniu
daniu

Reputation: 15008

Use a regular expression.

// either "02" or "2 and any number between 1 and 9"
Pattern p = Pattern.compile("(02|2[1-9]).*");
if (inputEan12.length() == 12) {
    if (p.matcher(inputEan).matches()) {
        // code 
    }
}

Upvotes: 0

Related Questions