English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.-
-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 Number | String Methods and Descriptions |
---|---|
1 | This method returns the length of a specific string. |
2 | This method returns a boolean value to determine whether one string is equal to another string. |
3 | This method merges2Concatenates a string and returns the concatenated string. |
4 | This method returns the index position of the character in the string. |
5 | This method returns the index position of the substring in the string. |
6 | This method returns a substring from the original string based on the starting position and the number of characters from the starting position. |
7 | 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....."
This method returns a substring from the right side of the string based on the number of characters.
right(str1,number)
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.
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"
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.
right(str1,number,$character)
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.
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"
This method returns the string in lowercase.
to_lower(str1)
str1 −This is the string that needs to be converted to lowercase.
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"
This method returns the string in uppercase.
to_upper(str1)
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"
Returns a substring of the String from the sub-position Start to the end of the string, or to the Stop position (inclusive).
sub_string(str1,start,stop)
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
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"