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 −
-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 −
“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 |
The method returns the length of a particular string. |
2 |
The method returns a Boolean value on whether one string is equal to another. |
3 |
The method concats 2 strings and returns the concatenated string. |
4 |
The method returns the index position of a character in a string. |
5 |
The method returns the index position of a sub string in a string. |
6 |
The method returns the sub string from the original string based on the starting position and number of characters from the starting position. |
7 |
The method returns the sub string from the original string based on the starting position and number of characters from the starting position. |
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.
left(str1,number,$character)
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.
Returns the sub string from the original string based on the left hand side of the string and the number.
-module(helloworld). -import(string,[left/3]). -export([start/0]). 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....."
The method returns the sub string from the right of the string based on the number of characters.
right(str1,number)
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.
Returns the substring from the original string based on the right hand side of the string and the number.
-module(helloworld). -import(string,[right/2]). -export([start/0]). 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”
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.
right(str1,number,$character)
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.
Returns the sub string from the original string based on the right hand side of the string and the number.
-module(helloworld). -import(string,[right/3]). -export([start/0]). 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"
The method returns the string in lower case.
to_lower(str1)
str1 − This is the string from which needs to be converted to lower case.
Returns the string in lower case.
-module(helloworld). -import(string,[to_lower/1]). -export([start/0]). 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"
The method returns the string in upper case.
to_upper(str1)
str1 − This is the string from which needs to be converted to upper case.
Return Value − Returns the string in upper case.
-module(helloworld). -import(string,[to_upper/1]). -export([start/0]). 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"
Returns a substring of String, starting at the position Start to the end of the string, or to and including the Stop position.
sub_string(str1,start,stop)
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
Returns a substring of String, starting at the position Start to the end of the string, or to and including the Stop position.
-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]).
When we run the above program, we will get the following result.
"hello"