In this guide, we will discuss String in ELM Programming Language. 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.
> "Adglob" "Adglob" : String > location = "Hyderabad" --variable "Hyderabad" : String > location "Hyderabad" : String >
String Functions
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 |
isEmpty
This function can be used to determine if a string is empty. This function returns True if the supplied String is empty.
Syntax
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 â
Illustration
> String.isEmpty "" True : Bool > String.isEmpty "Adglob" False : Bool > location = "Hyderabad" "Hyderabad" : String > String.isEmpty location False : Bool
reverse
This function reverses a string.
Syntax
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 â
Illustration
> String.reverse "Adglob" "bolgdA" : String
length
This function returns the length of a string.
Syntax
String.length String_value
To check the signature of function, type the following in elm REPL â
> String.length <function-> : String -> Int
The signature of the function shows Int as return type and input type as String.
Illustration
> String.length "Mohtashim" 9 : Int
append
This function returns a new string by appending two strings.
Syntax
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
Illustration
> String.append "Adglob" "Infosystem" AdglobInfosystem : String
concat
This function returns a new string by concatenating many strings into one.
Syntax
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
Illustration
> String.concat ["Hello","Adglob","Infosystem"] HelloAdglobInfosystem : String
split
This function splits a string using a given separator.
Syntax
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.
Illustration
> String.split "," "Hello,Adglob,Infosystem" ["Hello","Adglob","Infosystem"] : List String
slice
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.
Syntax
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.
Illustration
> String.slice 0 5 "Adglob" "Adglo" : String
contains
This function returns a True if the second string contains the first one.
Syntax
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
Illustration
> String.contains "Infosystem" "Adglob" True : Bool
toInt
This function converts a string into an int.
Syntax
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.
Illustration
> 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
toFloat
This function converts a string into a float.
Syntax
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.
Illustration
> 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
fromChar
This function creates a string from a given character.
Syntax
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
Illustration
> String.fromChar 'c' "c" : String
toList
This function converts a string to a list of characters.
Syntax
String.toList string_value
To check the signature of the function, type the following in elm REPL â
> String.toList <function> : String -> List Char
The signatures show function returns a list of characters and takes input a string.
Illustration
> String.toList "adglob" ['a','d','g','l','o','b'] : List Char
fromList
This function converts a list of characters into a String.
Syntax
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.
Illustration
> String.fromList ['h','e','l','l','o'] "hello" : String
toUpper
This function converts a string to all upper case.
Syntax
String.toUpper String_value
To check the signature of function, type the following in elm REPL â
> String.toUpper <function> : String -> String
Illustration
> String.toUpper "hello" "HELLO" : String
toLower
This function converts a string to all lower case.
Syntax
String.toLower String_value
To check the signature of function, type the following in elm REPL â
> String.toLower <function> : String -> String
Illustration
> String.toLower "AbCd" "abcd" : String
trim
This function gets rid of whitespace on both sides of a string.
Syntax
String.trim String_value
To check the signature of function, type the following in elm REPL â
> String.trim <function> : String -> String
Illustration
> String.trim "adglob " "adglob" : String
filter
This function filters a set of characters from input String. Keep only the characters that pass the test.
Syntax
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.
Illustration
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
map
This function takes a String and transforms every character in a string.
Syntax
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
Illustration
The following example replaces the character o with @ â
> String.map (\c -> if c == 'o' then '@' else c) "Adglob" "Adgl@b" : String
Next Topic : Click Here
Pingback: Elm - Functions | Adglob Infosystem Pvt Ltd
Pingback: beer777
Pingback: āšā¸ā¸´āšā¸Ąā¸ā¸šāšā¸ā¸´ā¸ā¸ā¸˛ā¸Ą