English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Strings in Erlang

In Erlang, a string literal can be constructed by enclosing string text in quotes. To construct a string in Erlang, double quotes must be used (e.g., "Hello World").

The following is an example of using strings in Erlang.-

Example

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

The above example creates a string named Str1 string variables. The string "This is a string" has been assigned to the variable and displayed accordingly.

The output of the above program will be:

"This is a string"

Next, we will discuss variousOperations available for Strings. Please note that for string operations, you also need to include the string library.

Serial NumberString Methods and Descriptions
1

len

This method returns the length of a specific string.

2

equal

This method returns a boolean value to determine whether one string is equal to another string.

3

concat

This method merges2Concatenates a string and returns the concatenated string.

4

chr

This method returns the index position of the character in the string.

5

str

This method returns the index position of the substring in the string.

6

substr

This method returns a substring from the original string based on the starting position and the number of characters from the starting position.

7

left

This method returns a substring from the original string based on the starting position and the number of characters from the starting position.

-module(helloworld). 
-import(string,[left/3]). 
-export([start/ 
start() -> 
   Str1 = "hello", 
   Str2 = left(Str1,10,$.), 
   io:fwrite("~p~n",[Str2]).

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

"hello....."

right

This method returns a substring from the right side of the string based on the number of characters.

Syntax

right(str1,number)

Parameters

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

  • Number −This is the number of characters that need to appear in the substring.

Return Value

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

-module(helloworld). 
-import(string,[right/2]). 
-export([start/ 
start() -> 
   Str1 = "hello World" 
   Str2 = right(Str1,2), 
   io:fwrite("~p~n",[Str2]).

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

"ld"

right with trailing character

This method returns a substring from the right side of the string based on the number of characters. However, if the number is greater than the length of the string, you can choose to include the trailing character.

Syntax

right(str1,number,$character)

Parameters

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

  • Number −This is the number of characters that need to appear in the substring.

  • $Character −Contains the ending character.

Return Value

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

-module(helloworld). 
-import(string,[right/3]). 
-export([start/ 
start() -> 
   Str1 = "hello", 
   Str2 = right(Str1,10,$.), 
   io:fwrite("~p~n",[Str2]).

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

".....hello"

to_lower

This method returns the string in lowercase.

Syntax

to_lower(str1)

Parameters

  • str1 −This is the string that needs to be converted to lowercase.

Return Value

Returns the string in lowercase.

-module(helloworld). 
-import(string,[to_lower/1]). 
-export([start/ 
start() -> 
   Str1 = "HELLO WORLD", 
   Str2 = to_lower(Str1), 
   io:fwrite("~p~n",[Str2]).

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

"hello world"

to_upper

This method returns the string in uppercase.

Syntax

to_upper(str1)

Parameters

  • str1 −This is the string that needs to be converted to uppercase.

  • Return Value −Returns an uppercase string.

-module(helloworld). 
-import(string,[to_upper/1]). 
-export([start/ 
start() -> 
   Str1 = "hello world", 
   Str2 = to_upper(Str1), 
   io:fwrite("~p~n",[Str2]).

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

"HELLO WORLD"

sub_string

Returns a substring of the String from the sub-position Start to the end of the string, or to the Stop position (inclusive).

Syntax

sub_string(str1,start,stop)

Parameters

  • str1 −This is the String from which the substring needs to be returned.

  • start −This is the starting position of the substring

  • stop −This is the stopping position of the substring

Return Value

Returns a substring of the String from the sub-position Start to the end of the string, or to the Stop position (inclusive).

-module(helloworld). 
-import(string,[sub_string/3]). 
-export([start/ 
start() -> 
   Str1 = "hello world", 
   Str2 = sub_string(Str1,1,5), 
   io:fwrite("~p~n",[Str2]).

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

"hello"