Elm - String

A sequence of Unicode characters is called a String. In Elm, strings are enclosed in "" double quotes. A String is a chunk of text as shown below.
> "TutorialsPoint"
"TutorialsPoint" : String
> location = "Hyderabad" --variable
"Hyderabad" : String
> location
"Hyderabad" : String
>
Some common functions that can be used to query or manipulate string values are given below. Use REPL to try the examples given below.
This function can be used to determine if a string is empty. This function returns True if the supplied String is empty.
String.isEmpty String_value

To check the signature of function, type the following in elm REPL −
> String.isEmpty
<function> : String -> Bool

Signature of the function shows Bool as return type and input type as String −
> String.isEmpty ""
True : Bool
> String.isEmpty "Tutorialspoint"
False : Bool
> location = "Hyderabad"
"Hyderabad" : String
> String.isEmpty location
False : Bool
This function reverses a string.
String.reverse String_value

To check the signature of function, type the following in elm REPL −
> String.reverse
<function> : String -> String

Signature of the function shows String as return type and input type as String −
> String.reverse "TutorialsPoint"
"tnioPslairotuT" : String
This function returns the length of a string.
String.length String_value

To check the signature of function, type the following in elm REPL −
> String.length
<function-> : String -> Int

Signature of the function shows Int as return type and input type as String.

> String.length "Mohtashim"
9 : Int
This function returns a new string by appending two strings.
String.append String_value1 String_value2

To check the signature of function, type the following in elm REPL −
> String.append
<function-> : String -> String -> String

Signature of shows two String input parameters and one String output parameter

> String.append "Tutorials" "Point"
TutorialsPoint : String
This function returns a new string by concatenating many strings into one.
String.concat [String1,String2,String3]

To check the signature of function, type the following in elm REPL −
> String.concat
<function> : List String -> String

Signature of shows a List of String input parameter and String return type

> String.concat ["Hello","Tutorials","Point"]
HelloTutorialsPoint : String
This function splits a string using a given separator.
String.split string_seperator String_value

To check the signature of function, type the following in elm REPL −
> String.split
<function> : String -> String -> List String

Signature of shows two input String parameters and output as a list of string type.

> String.split "," "Hello,Tutorials,Point"
["Hello","Tutorials","Point"] : List String
This function returns a substring given a start and end index. Negative indexes are taken starting from the end of the list. The value of the index starts from zero.
String.slice start_index end_index String_value

To check the signature of function, type the following in elm REPL −
> String.slice
<function> : Int -> Int -> String -> String

Signature of shows three input parameter and one return type.

> String.slice 0 13 "TutorialsPoint"
"TutorialsPoin" : String
This function returns a True if the second string contains the first one.
String.contains string1 string2

To check the signature of function, type the following in elm REPL −
> String.contains
<function> : String -> String -> Bool

Signature of shows bool return type and two input parameters

> String.contains "Point" "TutorialsPoint"
True : Bool
This function converts a string into an int.
String.toInt string_value

To check the signature of function, type the following in elm REPL −
> String.toInt
<function> : String -> Result.Result String Int

Since toInt can return error, the return type is Result, which is String or Int.

> String.toInt "20"
Ok 20 : Result.Result String Int
> String.toInt "abc"
Err "could not convert string 'abc' to an Int" : Result.Result String Int
This function converts a string into a float.
String.toFloat string_value

To check the signature of function, type the following in elm REPL −
> String.toFloat
<function> : String -> Result.Result String Float

Since toFloat can return error, the return type is Result, which is String or Float.

> String.toFloat "20.50"
Ok 20.5 : Result.Result String Float
> String.toFloat "abc"
Err "could not convert string 'abc' to a Float" : Result.Result String Float
This function creates a string from a given character.
String.fromChar character_value

To check the signature of function type following in elm REPL −
> String.fromChar
<function> : Char -> String

The signature shows String as return type and input as Char type

> String.fromChar 'c'
"c" : String
This function converts a string to a list of characters.
String.toList string_value

To check the signature of function, type the following in elm REPL −
> String.toList
<function> : String -> List Char

The signatures shows function returns a list of characters and takes input a string.

> String.toList "tutorialspoint"
['t','u','t','o','r','i','a','l','s','p','o','i','n','t'] : List Char
This function converts a list of characters into a String.
String.fromList list_of_characters

To check the signature of function, type the following in elm REPL −
> String.fromList
<function> : List Char -> String

The signatures shows function returns a list of characters and takes input a string.

> String.fromList ['h','e','l','l','o']
"hello" : String
This function converts a string to all upper case.
String.toUpper String_value

To check the signature of function, type the following in elm REPL −
> String.toUpper
<function> : String -> String

> String.toUpper "hello"
"HELLO" : String
This function converts a string to all lower case.
String.toLower String_value

To check the signature of function, type the following in elm REPL −
> String.toLower
<function> : String -> String

> String.toLower "AbCd"
"abcd" : String
This function gets rid of whitespace on both sides of a string.
String.trim String_value

To check the signature of function, type the following in elm REPL −
> String.trim
<function> : String -> String

> String.trim "tutorialspoint "
"tutorialspoint" : String
This function filters a set of characters from input String. Keep only the characters that pass the test.
String.filter test_function string_value

To check the signature of function, type the following in elm REPL −
> String.filter
<function> : (Char -> Bool) -> String -> String

The signature shows filter takes two input parameters and returns a String. The first parameter is a function, which has input Char and returns Bool.
In the example, we are passing Char.isUpper as parameter to filter method; it returns all upper-case characters as shown below.
> import Char
> String.filter Char.isUpper "abcDEF"
"DEF" : String
This function takes a String and transforms every character in a string.
String.filter mapping_function string_value

To check the signature of function, type the following in elm REPL −
> String.map
<function> : (Char -> Char) -> String -> String

The following example replaces the character o with @ −
> String.map (\c -> if c == 'o' then '@' else c) "TutorialsPoint"
"Tut@rialsP@int" : String

    27 Lectures 
     2.5 hours 

 Ahmed Elfakharany
 Print
 Add Notes
 Bookmark this page