How could I check if a string contains exactly one char?
Example:
strings
→ check for 1 i
→ true
strings
→ check for 1 s
→ false
I've tried to use Contains
but it checks for 1 or more.
You could use this Linq query to solve it.
"strings".Where(c => c == 's').Count() == 1 // gives false
"strings".Where(c => c == 'i').Count() == 1 // gives true
Explanaition:
The Where
method ask for a lamdba expression that checks if a char (variable c
) of the given string (strings
) is equal to respectively s
or i
and returns a list of chars that are equal to the condition.
The Count
method counts the results of that list.
Finaly just check if the result is equal to one.