• Home
  • HTML
    • HTML Introduction
    • HTML Basic
    • HTML Elements
    • HTML Attributes
    • HTML Headings
    • HTML Paragraphs
    • HTML Text Formatting
    • HTML Styles
    • HTML Comments
    • HTML Styles - CSS
    • HTML Links
    • HTML Images
    • HTML Tables
    • HTML Lists
    • HTML Block and Inline Elements
    • HTML class Attribute
    • HTML Forms
    • HTML Media
  • CSS
    • Introduction of CSS
    • CSS Syntax
    • CSS Selectors
    • How To Add CSS
    • CSS Comments
    • CSS Colors
    • CSS Backgrounds
    • CSS Borders
    • CSS Margins
    • CSS Text
    • CSS Lists
    • CSS Tables
    • CSS Box Model
    • CSS Dimension
    • CSS Padding
    • CSS Border
    • CSS Margin
    • CSS Outline
    • CSS Cursors
    • CSS Overflow
    • CSS Units
    • CSS Visual Formatting
    • CSS Display
    • CSS Visibility
    • CSS Position
    • CSS Layers
    • CSS Float
    • CSS Alignment
    • CSS Pseudo-classes
    • CSS Pseudo-elements
    • CSS Media Types
    • CSS Sprites
    • CSS Opacity
    • CSS Attribute Selectors
    • CSS Validation
    • CSS3 Border
    • CSS3 Gradients
    • CSS3 Text Overflow
  • JavaScript
    • JS Introduction
    • JS Getting Started
    • JS Syntax
    • JS Variables
    • JS Generating Output
    • JS Data Types
    • JS Operators
    • JS Events
    • JS Strings
    • JS Numbers
    • JS If Else
    • JS Switch Case
    • JS Arrays
    • JS Sorting Arrays
    • JS Loops
    • JS Functions
    • JS Objects
    • JS DOM Nodes
    • JS DOM Selectors
    • JS DOM Styling
    • JS DOM Get Set Attributes
    • JS DOM Manipulation
    • JS DOM Navigation
    • JS Window
    • JS Screen
    • JS Location
    • JS History
    • JS Navigator
    • JS Dialog Boxes
    • JS Timers
    • JS Date and Time
    • JS Math Operations
    • JS Type Conversions
    • JS Event Listeners
    • JS Event Propagation
    • JS Borrowing Methods
    • JS Hoisting Behavior
    • JS Closures
    • JS Strict Mode
    • JS JSON Parsing
    • JS Error Handling
    • JS Regular Expressions
    • JS Form Validation
    • JS Cookies
    • JS AJAX Requests
    • JS ES6 Features
  • jQuery
    • jQuery Introduction
    • jQuery Syntax
    • jQuery Selectors
    • jQuery Events
    • jQuery Show/Hide
    • jQuery Fade
    • jQuery Slide
    • jQuery Animation
    • jQuery Stop
    • jQuery Chaining
    • jQuery Callback
    • jQuery Get/Set
    • jQuery Insert
    • jQuery Remove
    • jQuery CSS Classes
    • jQuery Style Properties
    • jQuery Dimensions
    • jQuery Traversing
    • jQuery Ancestors
    • jQuery Descendants
    • jQuery Siblings
    • jQuery Filtering
    • jQuery Ajax
    • jQuery Load
    • jQuery Get/Post
    • jQuery No-Conflict
  • PHP
    • PHP Introduction
    • PHP Install
    • PHP Syntax
    • PHP Comments
    • PHP Variables
    • PHP Echo / Print
    • PHP Data Types
    • PHP Strings
    • PHP Constants
    • PHP Operators
    • PHP If...Else...Elseif
    • PHP Switch
    • PHP Loops
    • PHP Functions
    • PHP Arrays
    • PHP Superglobals
    • PHP Date and Time
    • PHP Include
    • PHP File Handling
    • PHP File Upload
    • PHP Cookies
    • PHP Sessions
    • PHP Filters
    • PHP Callback Functions
    • PHP JSON
    • PHP Exceptions
    • PHP What is OOP
    • PHP Classes/Objects
    • PHP Constructor
    • PHP Destructor
    • PHP Access Modifiers
    • PHP Inheritance
    • PHP Abstract Classes
    • PHP Interfaces
    • PHP Traits
    • PHP Static Methods
    • PHP Namespaces
  • SQL
    • Introduction to SQL
    • SQL Create Command
    • SQL ALTER Command
    • SQL Truncate Drop Rename
    • INSERT SQL command
    • UPDATE SQL command
    • DELETE SQL command
    • SQL COMMIT command
    • SQL ROLLBACK command
    • SQL GRANT and REVOKE Command
    • SQL WHERE clause
    • SQL LIKE clause
    • SQL ORDER BY Clause
    • SQL Group By Clause
    • SQL HAVING Clause
    • SQL DISTINCT keyword
    • SQL AND OR operator
    • SQL Constraints
    • SQL Functions
    • SQL JOIN
  • Python
    • Getting started with Python
    • Introduction to IDLE
    • Python 2.x vs. Python 3.x
    • Syntax Rules and First Program
    • Numbers and Math Functions
    • Python Operators
    • Python Variables
    • Python Modules and Functions
    • Python Input and Output
    • Data Types in Python
    • String in Python
    • String Functions in python
    • Lists in Python
    • Utilizing List Elements by Iterating
    • Deleting List Elements & other Functions
    • Dictionaries in Python
    • Functions for Dictionary
    • Tuples in Python
    • Relational and Logical Operators
    • Conditional Statements in Python
    • Looping in Python
    • Define Functions in Python
    • Python-Introduction to OOP
    • Object Oriented Programming in Python
    • Classes in Python
    • The concept of Constructor
    • Destructors - Destroying the Object in Python
    • Inheritance in Python
    • Access Modifers in Python
    • Types of Inheritance
    • Method Overriding in Python
    • Polymorphism
    • static Keyword
    • Operator Overloading Python
    • Introduction to Error Handling
    • Exception Handling: try and except
    • Exeption Handling: finally
    • Exception Handling: raise
    • File Handling
    • Reading and Writing File
    • Introduction to Multithreading
    • Threading Module in Python
    • Thread Object
    • Lock Object
    • RLock Object
    • Event Object
    • Timer Object
    • Condition Object
    • Barrier Object
    • __name__ Variable in Python
    • Iterable and Iterator
    • yield Keyword
    • Python Generators
    • Python Closures
    • Python Decorators
    • @property Decorator in Python
    • Assert Statement
    • Garbage Collection
    • Shallow and Deep Copy
    • Introduction to Logging
    • Configure Log LEVEL, Format etc
    • Python Logging in a file
    • Python Logging Variable Data
    • Python Logging Classes and Functions
    • Python MySQL Introduction
    • Create Database - Python MySQL
    • Create Table - Python MySQL
    • Insert Data in Table
    • Select Data from Table
    • Update data in Table
    • Delete data from Table
    • Drop Table from Database
    • WHERE clause - Python MySQL
    • Order By clause - Python MySQL
    • Limit clause - Python MySQL
    • Table Joins - Python MySQL
  • MongoDB
    • MongoDB Introduction
    • Overview of MongoDB
    • MongoDB vs SQL Databases
    • Advantages of MongoDB
    • When to go for MongoDB
    • Data Modelling in MongoDB
    • Is MongoDB really Schemaless?
    • Installing MongoDB on Windows and Linux
    • Datatypes in MongoDB
    • Create and Drop Database in MongoDB
    • MongoDB: Creating a Collection
    • CRUD Operations in MongoDB
    • Data Relationships in MongoDB
    • Indexing in MongoDB
    • Sorting in MongoDB
    • Aggregation in MongoDB
    • Data Backup and Restoration in MongoDB
    • Sharding in MongoDB
    • Java Integration with MongoDB
  • Elixir
    • Elixir Overview
    • Elixir Environment
    • Elixir Basic Syntax
    • Elixir Data Types
    • Elixir Variables
    • Elixir Operators
    • Elixir Pattern Matching
    • Elixir Decision Making
    • Elixir Strings
    • Elixir Char Lists
    • Elixir Lists and Tuples
    • Elixir Keyword Lists
    • Elixir Maps
    • Elixir Modules
    • Elixir Aliases
    • Elixir Functions
    • Elixir Recursion
    • Elixir Loops
    • Elixir Enumerables
    • Elixir Streams
    • Elixir Structs
    • Elixir Protocols
    • Elixir File I/O
    • Elixir Processes
    • Elixir Sigils
    • Elixir Comprehensions
    • Elixir Typespecs
    • Elixir Behaviours
    • Elixir Errors Handling
    • Elixir Macros
    • Elixir Libraries
  • TypeScript
    • TypeScript Overview
    • Install TypeScript
    • First TypeScript Program
    • Type Annotation
    • TypeScript Variable
    • TypeScript Data Type Number
    • TypeScript Data Type String
    • TypeScript Data Type Boolean
    • TypeScript Arrays
    • TypeScript Tuples
    • TypeScript Enum
    • TypeScript Union
    • TypeScript Any Data Type
    • TypeScript Void Data Type
    • TypeScript Never Data Type
  • Home
  • Elixir Overview
  • Elixir Environment
  • Elixir Basic Syntax
  • Elixir Data Types
  • Elixir Variables
  • Elixir Operators
  • Elixir Pattern Matching
  • Elixir Decision Making
  • Elixir Strings
  • Elixir Char Lists
  • Elixir Lists and Tuples
  • Elixir Keyword Lists
  • Elixir Maps
  • Elixir Modules
  • Elixir Aliases
  • Elixir Functions
  • Elixir Recursion
  • Elixir Loops
  • Elixir Enumerables
  • Elixir Streams
  • Elixir Structs
  • Elixir Protocols
  • Elixir File I/O
  • Elixir Processes
  • Elixir Sigils
  • Elixir Comprehensions
  • Elixir Typespecs
  • Elixir Behaviours
  • Elixir Errors Handling
  • Elixir Macros
  • Elixir Libraries
Home >> elixir >> Elixir Strings

Elixir Strings

Welcome to a tutorial on Elixir. Here you will learn about Strings in Elixir.

In Elixir, Strings are inserted between double quotes, and they are encoded in UTF-8. This is quite de-similar from C and C++ in which the default strings are ASCII encoded and only 256 different characters are possible, as the UTF-8 consists of 1,112,064 code points. It means that UTF-8 encoding consists of those many different possible characters. But, we can use symbols such as ö, ł, etc, since the strings use utf-8.

 

Create a String

Let’s create a string variable, by simply assigning a string to a variable, as shown below.

str = "Hello world"

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

str = str = "Hello world" 
IO.puts(str)

And the output is:

Hello World

 

Empty Strings

Here we can create an empty string by using the string literal, "". Check the example below.

a = ""
if String.length(a) === 0 do
   IO.puts("a is an empty string")
end

The output is:

a is an empty string

 

String Interpolation

String interpolation is a simple way of constructing a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Thus, Elixir supports string interpolation, so to make use of a variable in a string, when writing it, wrap it with curly braces and prepend the curly braces with a '#' sign. This is shown below.

x = "Justin" 
y = "My Name is #{x}"
IO.puts(y)

the above code takes the value of x and substitutes it with y. The output will look like this:

My Name is Justin

 

String Concatenation

You already learned about String concatenation in the previous tutorial, where the '<>' operator is used to concatenate strings in Elixir. Check out the example on how to concatenate 2 strings,

x = "Justin"
y = "Drake"
z = x <> " " <> y
IO.puts(z)

The output is:

Justin Drake

 

String Length

We can use the String.length function to obtain the length of a string, by simply passing the string as a parameter and the size will be displayed. Check out the example below.

IO.puts(String.length("Hello"))

The output is:

5

 

Reversing a String

We can reverse a string by simply passing it to the String.reverse function. This is shown below.

IO.puts(String.reverse("Elixir"))

The output is:

rixilE

 

String Comparison

Now to compare 2 strings, the == or the === operators can be used. This is shown below:

var_1 = "Hello world"
var_2 = "Hello Elixir"
if var_1 === var_2 do
   IO.puts("#{var_1} and #{var_2} are the same")
else
   IO.puts("#{var_1} and #{var_2} are not the same")
end

The output is: 

Hello world and Hello elixir are not the same.

 

String Matching

You have already known the use of the =~ string match operator. Now, to confirm if a string matches a regex, is the string match operator or the String.match? the function can be used. Check out the example below.

IO.puts(String.match?("foo", ~r/foo/))
IO.puts(String.match?("bar", ~r/foo/))

The output is: 

true 
false

Also, this can be achieved by using the =~ operator, as shown below.

IO.puts("foo" =~ ~r/foo/)

The output is: 

true

 

String Functions

Elixir supports a large number of functions related to strings, the table below shows a few of the most used functions and their purpose.

Sr.No.Function and its Purpose
1at(string, position): This returns the grapheme at the position of the given utf8 string. If position is greater than string length, then it returns nil
2capitalize(string): This converts the first character in the given string to uppercase and the remainder to lowercase
3contains?(string, contents): This checks if a string contains any of the given contents
4downcase(string): This converts all characters in the given string to lowercase
5ends_with?(string, suffixes): This returns true if a string ends with any of the suffixes given
6first(string): This returns the first graphene from a utf8 string, nil if the string is empty
7last(string): This returns the last grapheme from a utf8 string, nil if the string is empty
8replace(subject, pattern, replacement, options \ []): This returns a new string created by replacing occurrences of pattern in subject with replacement
9slice(string, start, len): This returns a substring starting at the offset start and of length len
10split(string): This divides a string into substrings at each Unicode whitespace occurrence with leading and trailing whitespace ignored. The groups of whitespace are treated as a single occurrence. However, divisions do not occur on non-breaking whitespace
11upcase(string): This converts all characters in the given string to uppercase

 

Binaries

A binary is simply a sequence of bytes. Binaries are defined using << >>, as shown below.

<< 0, 1, 2, 3 >>

But, interestingly, those bytes can be organized in any way, even in a sequence that does not make them a valid string. Check out the example below.

<< 239, 191, 191 >>

But, Strings are also binaries, also, the string concatenation operator <> is a Binary concatenation operator: check out the example below

IO.puts(<< 0, 1 >> <> << 2, 3 >>)

The output is:

<< 0, 1, 2, 3 >>

Note that the ł character representation takes up 2 bytes since it is utf-8 encoded.

Now, since each number represented in a binary is meant to be a byte when this value goes up from 255, it will be truncated. But, to prevent this, we can make use of a size modifier to specify how many bits we want that number to take. Check out the example below.

IO.puts(<< 256 >>) # truncated, it'll print << 0 >>
IO.puts(<< 256 :: size(16) >>) #Takes 16 bits/2 bytes, will print << 1, 0 >>

The output is:

<< 0 >>
<< 1, 0 >>

Also, we use the utf8 modifier, if a character is a code point then, it will be produced in the output or else the bytes. This is shown below.

IO.puts(<< 256 :: utf8 >>)

The output is:

Ā

In addition, we have a function called is_binary that checks if a given variable is a binary. But, take note that only variables which are stored as multiples of 8bits are binaries.

 

Bitstrings

If we define a binary by making use of the size modifier and passing it a value that is not a multiple of 8, we end up having a bitstring rather than a binary. Check out the example below.

bs = << 1 :: size(1) >>
IO.puts(bs)
IO.puts(is_binary(bs))
IO.puts(is_bitstring(bs))

The output is:

<< 1::size(1) >>
false
true

This means that variable bs is not a binary, but rather a bitstring. Also, we can say that a binary is a bitstring where the number of bits is divisible by 8. Interestingly, pattern matching works even on binaries and on bitstrings in the same manner.

 

  • Prev
  • Next


-Advertisement-


DeveloperTutorial
[email protected] © 2022-2023 Developers Tutorial All rights reserved.

Follow Us

Facebook Twitter LinkedIn Printerest Reddit

Announcement

Its a big achivement for us, We make a partnership with TutorialWithExample.com for the better content of our users.

Still Need Help ?

Let us now about your issue and a Professional will reach you out.