Search code examples
elm

Compare char to string in Elm


I'd like to loop through the characters of a string and see if each is contained in another string. However, String.toList returns a list of Chars, not Strings, which isn't allowed by the String.contains function. Passing the Char to toString doesn't help, is there another way to achieve this goal, or do I just need another approach altogether?

> String.contains (toString 'a') "asdf"
False : Bool

Solution

  • Use String.fromChar to convert a character to string.

    String.fromChar 'a' -- "a"
    String.contains (String.fromChar 'a') "asdf" -- True