Elixir Strings - Elixir

How to create strings in Elixir?

Strings in Elixir are always inserted in double quotes and they are encoded in UTF-8. UTF-8 consists of 66536 code points. This means that UTF-8 encoding consists of those many different possible characters. As strings use utf-8, symbols like: ö, ł, etc can also be used.

Create a String

To create a string variable, just assign a variable a string:

To print this to the console, just call IO.puts function and pass it the variable str:

While running above program, it produces following result:

Empty Strings

An empty string can be created using the string literal, "". For example,

While running above program, it produces following result

String Interpolation

String interpolation is a way of constructing a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Elixir supports string interpolation for using a variable in a string. While writing it, wrap it with curly braces and prepend the curly braces with a '#' sign. For example:

This will take the value of x and substitute it in y. While running above program, it produces following result:

String Concatenation

'<>' operator is used to concatenate strings in Elixir. To concatenate 2 strings,

While running above program, it produces following result:

String Length

To get the length of the string, use String.length function. Pass a string as a parameter and it will show its size. For example,

While running above program, it produces following result: 5.

Reversing a string

To reverse a string, pass it to the String.reverse function. For example,

While running above program, it produces following result:

String comparison

To compare 2 strings, use == or === operators. For example,

While running above program, it produces following result:

String Matching

To check if a string matches a regex, you can either use the string match operator or the String.match? function. For example,

While running above program, it produces following result:

Same can also be achieved with the =~ operator. For example,

While running above program, it produces following result:

String Functions

Elixir supports a huge number of strings functions and few of them which are frequently used are listed here. For more info on them, please visit the Elixir docs.

S.No.

Function and its purpose

1

at(string, position)

Returns the grapheme at the position of the given utf8 string. If position is greater than string length, then it will return nil

2

capitalize(string)

Converts first character in the given string to uppercase and the remainder to lowercase

3

contains?(string, contents)

Checks if a string contains any of the given contents

4

downcase(string)

Converts all characters in the given string to lowercase

5

ends_with?(string, suffixes)

Returns true if string ends with any of the suffixes given

6

first(string)

Returns the first grapheme from a utf8 string, nil if the string is empty

7

last(string)

Returns the last grapheme from a utf8 string, nil if the string is empty

8

replace(subject, pattern, replacement, options \\ [])

Returns a new string created by replacing occurrences of pattern in subject with replacement

9

slice(string, start, len)

Returns a substring starting at the offset start, and of length len

10

split(string)

Divides a string into substrings at each Unicode whitespace occurrence with leading and trailing whitespace ignored. Groups of whitespace are treated as a single occurrence. Divisions do not occur on non-breaking whitespace

11

upcase(string)

Converts all characters in the given string to uppercase

Binaries

A binary is just a sequence of bytes. Binaries are defined using << >>. For example:

These bytes can be organized in any way, even in a sequence that does not make them a valid string. For example,

Strings are also binaries and the string concatenation operator <> is actually a Binary concatenation operator:

While running above program, it produces following result:

Remember that as ł character is utf-8 encoded, this character representation will take up 2 bytes.

As each number represented in a binary is meant to be a byte, when this value goes up from 255, it will be truncated. To prevent this, you can use size modifier to specify how many bits you want that number to take. For example:

While running above program, it produces following result:

You can also use utf8 modifier, this will output the character if it is codepoint, else the bytes:

While running above program, it produces following result:

There is a function called is_binary which checks if a given variable is a binary. Note that only variables which are stored as multiple of 8bits are binaries.

Bitstrings

If you are defining a binary using the size modifier and pass it a value which is not a multiple of 8, we end up with a bitstring instead of a binary. For example,

While running above program, it produces following result:

This means that variable bs is not a binary but rather a bitstring. Binary can be a bitstring where the number of bits is divisible by 8. Pattern matching works on binaries as well as bitstrings in a similar way.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Elixir Topics