Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a string only contains certain characters in scala

How to check whether a string only contains certain characters, say "a" and "b" in Scala?
I found several resources online, they are using regex.
For Scala, most of them talked about the findAllIn or findFirstIn function. But I would like to have something that returns true or false.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

val s: String = ???

s.forall("ab".contains)

forall checks whether a condition is true for all elements of a collection. So in this case it checks whether a condition is true for each character in s.

contains checks whether a string contains a character, so "ab".contains(x) checks whether x is either ‘a’ or ‘b’.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading