Question

Right what I was trying to do is validate some input so that the input must start with 2 letters and followed by 3 digits, but I can't find a way of testing a string for this

 boolean test;
 String str;
 str.format ("%s%s%d%d%d") //used this to give what the format was and was going to use it with a boolean, tried it in an if statement such as

if str.format = ("%s%s%d%d%d") then
{
 test = true
}
else
{
 test = false
}

I'm wondering what I would have to do to achieve this?

Was it helpful?

Solution

Sounds like you want a regular expression...

// Do this once and cache it...
Pattern pattern = Pattern.compile("\\p{Alpha}{2}\\d{3}");

// Then test it:
boolean test = pattern.matcher(str).lookingAt();

See the Pattern documentation for more details (and alternatives to \p{Alpha}).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top