Search code examples
java

How to check if the field contains hyphen character in java


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.


Solution

  • 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("\\-")) { //We use \\- because - is a special character
        //code
    }