Currently you are not searching for a single date format in a list of date formats, you are searching for a date format String
within a single String
that contains multiple comma separated date formats.
Since that String
starts with "dd/MM/yyyy"
, which contains "dd/MM/yy"
, both contains
and indexOf
find a match (which, given your requirements, is a false positive).
In order to search for full date formats only (in your comma separated list), you should split your input String
into a List<String>
, and run contains of that List
:
Arrays.asList(validDateFormats.split(",")).contains("dd/MM/yy")
validDateFormats.split(",")
splits the input String
into a String array (using the comma as a separator). Arrays.asList
creates a List<String>
backed by that array, which allows you to run List
‘s contains
method to search for the "dd/MM/yy"
date format. This will return false
as expected.
6
solved Using String#contains() and String#indexOf() to locate a date format String in a comma separated list finds incorrect matches [closed]