Strings and Chars

The other basic types in Elm are super straightforward. We've already seen the String type. You can make a string literal in Elm with double quotes.

> "Hi there!"
"Hi there!" : String

The Char type is used to represent single characters. Character literals are made with single quotes instead of double quotes.

> 'q'
'q' : Char

Putting strings together

You can concatenate strings using the ++ operator.

> "Better " ++ "together"
"Better together" : String

Converting values to Strings

You can convert any value to a String with toString. The resulting string keeps some artifacts from its Elm type--notice how the Char's string includes the single quotes of a character literal.

> toString 42
"42" : String
> "The answer is: " ++ toString 42
"The answer is: 42" : String
> toString 'a'
"'a'" : String

Escaping special characters

If you try converting a String to a String, something interesting will happen:

> toString "hello"
"\"hello\"" : String

The backslashes are there to escape the double quotes within the String. This tells Elm that these double quotes are not there to end our String.

Similarly, a single quote must be escaped inside a character literal.

> '\''
'\'' : Char

Likewise, the backslash character itself must be escaped if you want to use it inside a string/character literal. Another escape sequence you'll come across often is \n, for the newline character.

results matching ""

    No results matching ""