How to check if the field contains a hyphen character in java

advertisements

My Object returns me - from Log.d("FormattedDate", Object.getDOB());

if (!Object.getDOB().matches("[^-]*")) {
    txtDOB.setText(Object.getDOB());
   } else {
    txtDOB.setText("-");
}

I am checking if my Object.getDOB() matches with -, then show emptry strings, but this regExp is not working.


java.lang.String has a String#contains() method that does this for you:

Returns true if and only if this string contains the specified sequence of char values.

if (Object.getDOB().contains("-")) {
    //code
}