Elm – String

elm string

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. NoMethodDescription
1isEmpty : String -> Boolchecks string is empty
2reverse : String -> Stringreverses a input string
3length : String -> Intreturns an integer length
4append :String -> String -> Stringappends two string and returns a new string
5append :String -> Sconcat : List String -> Stringappends a list of strings and returns a new string
6split : String -> String -> List Stringsplits an input string using a given separator, returns a string list
7slice : Int -> Int -> String -> Stringreturns a substring given a start , end index and input string
8contains : String -> String -> Boolreturns true if second string contains the first one
9toInt : String -> Result.Result String Intparses a String to Integer
10toInt : String -> Result.Result String Intparses a String to Integer
11toFloat : String -> Result.Result String Floatparses a String to float
12fromChar : Char -> Stringcreates a string from a given character.
13toList : String -> List Charconverts string to list of characters
14fromList : List Char -> Stringconverts a list of characters into a String
15toUpper : String -> Stringconverts input string to upper case
16trim : String -> Stringgets rid of whitespace on both sides of a string.
17filter : (Char -> Bool) -> String -> Stringfilters set of characters from input string
18map : (Char -> Char) -> String -> Stringtransforms 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

This Post Has 3 Comments

Leave a Reply