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.
> "Howcodex" "Howcodex" : 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.
Sr. No | Method | Description |
---|---|---|
1 | isEmpty : String -> Bool | checks string is empty |
2 | reverse : String -> String | reverses a input string |
3 | length : String -> Int | returns an integer length |
4 | append :String -> String -> String | appends two string and returns a new string |
5 | append :String -> Sconcat : List String -> String | appends a list of strings and returns a new string |
6 | split : String -> String -> List String | splits an input string using a given separator, returns a string list |
7 | slice : Int -> Int -> String -> String | returns a substring given a start , end index and input string |
8 | contains : String -> String -> Bool | returns true if second string contains the first one |
9 | toInt : String -> Result.Result String Int | parses a String to Integer |
10 | toInt : String -> Result.Result String Int | parses a String to Integer |
11 | toFloat : String -> Result.Result String Float | parses a String to float |
12 | fromChar : Char -> String | creates a string from a given character. |
13 | toList : String -> List Char | converts string to list of characters |
14 | fromList : List Char -> String | converts a list of characters into a String |
15 | toUpper : String -> String | converts input string to upper case |
16 | trim : String -> String | gets rid of whitespace on both sides of a string. |
17 | filter : (Char -> Bool) -> String -> String | filters set of characters from input string |
18 | map : (Char -> Char) -> String -> String | transforms every character in an input string |
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 "Howcodex" 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 "Howcodex" "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" Howcodex : 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"] HelloHowcodex : 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 "Howcodex" "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" "Howcodex" 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 "howcodex" ['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 "howcodex " "howcodex" : 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) "Howcodex" "Tut@rialsP@int" : String