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 text.contains() contains multiple values

I have a text edit controller and I would like to check mutliple characters in the same contains ()

_changeUsernameController.text.contains("a" "b") // what I want

_changeUsernameController.text.contains("a") ||
_changeUsernameController.text.contains("b") // what I have to do

I don’t want to write 50 lines so how can I write all in one line like the ‘what I want’ line
Thanks

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

>Solution :

You can use the any method to write less code for the very same effect:

final subStrings = <String>["a", "b" /* ... */ ];
var result = subStrings.any(_changeUsernameController.text.contains);

Or if you prefer it even shorter:

var result = ["a", "b"].any(_changeUsernameController.text.contains);
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