I'm running a comparison program and at the minute it does a direct 'string-to-string' comparison and if they are an exact match it outputs that they are a match.
Well, I was hoping to add an additional feature that allowed for 'similarity'...
so for example:
String em1 = "52494646";
String em2 = "52400646";
if (em1.equals(em2)){
output.writeUTF(dir + filenames[i]);
}
This is sort of a snippet of the code. I'd like it so that it skips over the "00" and still recognises it as 'almost' the same number and still outputs it.
I'd imagine it would look something like String em2 = "524"+ ## +"646"
but thats obviously just a concept
Does anyone know if there is a way to have this kind of 'wildcard' (a term I've picked up from uni SQL) or if there is another way to do this similarity type deal.
Thanks :)
You can use regular expressions:
if (em1.matches("524[0-9]{2}646")) {
// do stuff
}
For Java specific documentation see the Pattern
class. For some uses of regular expressions (such as in the sample above), there are shortcut methods in String
: matches()
, replaceAll()
/replaceFirst()
and split()
.
regular-expressions.info has good documentation on regular expression in general.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With