Erlang – Strings


Erlang – Strings


”;


A String literal is constructed in Erlang by enclosing the string text in quotations. Strings in Erlang need to be constructed using the double quotation marks such as “Hello World”.

Following is an example of the usage of strings in Erlang −

Example

-module(helloworld). 
-export([start/0]). 

start() ->
   Str1 = "This is a string", 
   io:fwrite("~p~n",[Str1]).

The above example creates a string variable called Str1. The string “This is a string” is assigned to the variable and displayed accordingly.

The output of the above program will be −

Output

“This is a string”

Next, we will discuss the various operations available for Strings. Note that for string operations, you need to include the string library as well.

Sr.No String Methods & Description
1

len

The method returns the length of a particular string.

2

equal

The method returns a Boolean value on whether one string is equal to another.

3

concat

The method concats 2 strings and returns the concatenated string.

4

chr

The method returns the index position of a character in a string.

5

str

The method returns the index position of a sub string in a string.

6

substr

The method returns the sub string from the original string based on the starting position and number of characters from the starting position.

7

left

The method returns the sub string from the original string based on the starting position and number of characters from the starting position.

left with trailing character

The method returns the sub string from the left of the string based on the number of characters. But with the option to include a trailing character if the number is greater than the length of the string.

Syntax

left(str1,number,$character)

Parameters

  • str1 − This is the string from which the sub string needs to be extracted.

  • Number − This is the number of characters which need to be present in the substring.

  • $Character − The character to include as the trailing character.

Return Value

Returns the sub string from the original string based on the left hand side of the string and the number.

For example

-module(helloworld). 
-import(string,[left/3]). 
-export([start/0]). 

start() -> 
   Str1 = "hello", 
   Str2 = left(Str1,10,$.), 
   io:fwrite("~p~n",[Str2]).

Output

When we run the above program, we will get the following result.

"hello....."

right

The method returns the sub string from the right of the string based on the number of characters.

Syntax

right(str1,number)

Parameters

  • str1 − This is the string from which the sub string needs to be extracted.

  • Number − This is the number of characters which need to be present in the substring.

Return Value

Returns the substring from the original string based on the right hand side of the string and the number.

For example

-module(helloworld). 
-import(string,[right/2]). 
-export([start/0]). 

start() -> 
   Str1 = "hello World", 
   Str2 = right(Str1,2), 
   io:fwrite("~p~n",[Str2]).

Output

When we run the above program, we will get the following result.

“ld”

right with trailing character

The method returns the substring from the right of the string based on the number of characters. But with the option to include a trailing character if the number is greater than the length of the string.

Syntax

right(str1,number,$character)

Parameters

  • str1 − This is the string from which the sub string needs to be extracted.

  • Number − This is the number of characters which need to be present in the substring.

  • $Character − The character to include as the trailing character.

Return Value

Returns the sub string from the original string based on the right hand side of the string and the number.

For example

-module(helloworld). 
-import(string,[right/3]). 
-export([start/0]). 

start() -> 
   Str1 = "hello", 
   Str2 = right(Str1,10,$.), 
   io:fwrite("~p~n",[Str2]).

Output

When we run the above program, we will get the following result.

".....hello"

to_lower

The method returns the string in lower case.

Syntax

to_lower(str1)

Parameters

  • str1 − This is the string from which needs to be converted to lower case.

Return Value

Returns the string in lower case.

For example

-module(helloworld). 
-import(string,[to_lower/1]). 
-export([start/0]). 

start() -> 
   Str1 = "HELLO WORLD", 
   Str2 = to_lower(Str1), 
   io:fwrite("~p~n",[Str2]).

Output

When we run the above program, we will get the following result.

"hello world"

to_upper

The method returns the string in upper case.

Syntax

to_upper(str1)

Parameters

  • str1 − This is the string from which needs to be converted to upper case.

  • Return Value − Returns the string in upper case.

For example

-module(helloworld). 
-import(string,[to_upper/1]). 
-export([start/0]). 

start() -> 
   Str1 = "hello world", 
   Str2 = to_upper(Str1), 
   io:fwrite("~p~n",[Str2]).

Output

When we run the above program, we will get the following result.

"HELLO WORLD"

sub_string

Returns a substring of String, starting at the position Start to the end of the string, or to and including the Stop position.

Syntax

sub_string(str1,start,stop)

Parameters

  • str1 − This is the string from which the sub string needs to be returned.

  • start − This is the start position of the sub string

  • stop − This is the stop position of the sub string

Return Value

Returns a substring of String, starting at the position Start to the end of the string, or to and including the Stop position.

For example

-module(helloworld). 
-import(string,[sub_string/3]). 
-export([start/0]). 

start() -> 
   Str1 = "hello world", 
   Str2 = sub_string(Str1,1,5), 
   io:fwrite("~p~n",[Str2]).

Output

When we run the above program, we will get the following result.

"hello"

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *