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

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

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

I check if my Object.getDOB () matches with - and then shows emptry lines, but this regExp does not work.

+4
source share
3 answers

java.lang.String has String # contains () which does this for you:

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

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

You can also use

 if (Object.getDOB().indexOf("-") != -1) { //code } 

if it returns -1 , then the string does not contain char (in your case, "-" ). Otherwise, it returns a char index.

+2
source

You can use contains() in java to search which is available in String class

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

 getDOB().contains("-") 

SEE HERE

+1
source

Source: https://habr.com/ru/post/1485775/


All Articles