Dual Classesstring
in C Plus Plus
The following table contains the most commonly used class functions string
in C Plus .
Function name and definition | |
---|---|
int length()
Returns a number representing the number of characters of the text that called it. To be precise, the number you return represents from how manyByteText composed. function length() |
|
int size()
Returns a number that represents the number of characters of the text that called it, just like a functionlength() .function size() |
|
string substr(int pos=0, int len = npos)
Returns text that is part of the text (Substring)who summoned her. In place of the parameter pos we pass index the character from which we want to start transcribing the text. Parameter location len You can pass a number representing how many characters you want to copy from the index parameter pos if you don't want to copy all the existing text.substr() function |
|
int find(string str)
Searching in the text that summoned her for the firstindexStarting with him, there is the same text that we pass to her in place of the parameter str and she returns it.find() function |
|
int rfind(string str)
Searches in the text that summoned her for anotherindexStarting with him, there is the same text that we pass to her in place of the parameter str and she returns it.rfind() function |
|
string replace(int pos, int len, string replacement)
Used to modify a specific part of the text that called it. Parameter location We pos pass index the character from which we want to start changing the text. Parameter location We len pass a number representing how many characters you want to change as of the index parameterpos .The location of the parameter We replacement pass the text to be added in place of the part to be deleted.function replace() |
|
void swap(string str)
Replace the content of the text that called it with the content of the text we pass in place of the parameterstr .It also replaces the content of the variable we pass in place of the parameter str with the content of the text that called it.swap() function |
|
string append (const string str)
It adds the value of the text we pass in place of the parameter str at the end of the text that called it and returns the resulting text when combined.append() function |
|
bool empty()
Returns true if the invoking text is not empty (ie, contains at least one character). And return false if not.empty () function |
|
void clear()
Clears all characters in the recalled text.clear() function |
|
int compare (const string str)
Compares the size of the text that called it with the size of the text we pass to it in place of the parameterstr .Returns 0 if they are the same size. Returns 1 if the recalled text has more characters. come back -1 if the recalled script has a smaller number of characters.compare() function |
|
string insert (int pos, const string str)
Adds the value of the text we pass in place of the parameterstr in the text that called it atIndexThe cell we specify in place of the parameter pos and returns the resulting text when merging them together.insert() function |
function length في السي بلاس بلاس
its definition
The Lehgh() function returns a number representing the number of characters of the called script.
To be precise, the number you return represents from how manyByteText composed.
Build function Lehgh
size_t length() const
size_t length() const noexcept
Parameters of the function Lehgh .()
Do not accept any parameters.
Return value of the Lehgh() function
Returns a number representing from how manyByteThe text is composed by the invoked.
First example of using the Lehgh() function
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "Welcome to alawiri.com";
// str The text of the variable Byte here consists of how many // 1Byte the answer would be 22 because every English character is stored in cout << "Length of str is " << str.length() << " bytes"; return 0; }
• We will get the following result when running.
Length of str is 22 bytes
The second example in the function Lehgh .()
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "Saddam"; // str The text of the variable Byte here consists of how many // 2Byte the answer will be 8 because every Arabic character is stored in cout << "Length of str is " << str.length() << " bytes"; return 0; }
• We will get the following result when running.
Length of str is 8 bytes
Function size()
in Cplus Plus
Explanation of the size . function
The size function returns a number representing the number of characters of the called text.
To be precise, the number you return represents from how manyByteText composed.
function structure size
size_t size() const
size_t size() const noexcept
Parameters of the function size .()
Do not accept any parameters.
Return value of the function size()
Returns a number representing from how manyByteThe text is composed by the invoked.
The first example on the function size()
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "Welcome to alawiri.com"; // str The text of the variable Byte here consists of how many // 1Byte the answer would be 22 because every English character is stored in cout << "Size of str is " << str.size(); return 0; }
• We will get the following result when running.
Size of str is 22
The second example is in the function size()
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "Saddam"; // str The text of the variable Byte here consists of how many // 2Byte the answer will be 8 because every Arabic character is stored in cout << "Size of str is " << str.size(); return 0; }
• We will get the following result when running.
Size of str is 8
Function substr()
in Cplus Plus
its definition
The substring() function returns the text of a substring statement (Substring)who summoned her.
Substr () function Define it The text of the statement returns a portion of the text (Substring) that called it.
When calling this function, we have two options:
Determine where to start.
Determine where to start and exactly how many letters we want to take.
If we do not specify how many characters we want to take from the text, it will return all existing text starting from a numbertheindexWhich we passed in place of the parameterpos
to anotherindexIt is in the text that called it.
Build the function substring()
There are two forms of this function. It can be called in two different ways.
string substr (size_t pos = 0, size_t len = npos) const
Parameters of the function substring()
In place of the parameter
pos
we passindex
the character from which we want to start transcribing the text.Parameter location
len
You can pass a number representing how many characters you want to copy from theindex
parameterpos
if you don't want to copy all the existing text.
Return value of the substring() function
come backString
ansubstrFromtheString
who summoned her.
Possible errors in the substring() function
std::out_of_range : if you pass a numberindexIt is not found in the text where the parameter pos
is. The following error will appear when you run the program.
Example of the function substring()
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "welcome to alawiri.com, best site for learning"; cout << str.substr(11) << endl; // No. 11 index All text found starting from the letter on the substr() here the function will return cout << str.substr(11, 7) << endl; // No. 11 index The first 7 characters in the text starting with substr() Here the function will return return 0; }
• We will get the following result when running.
alawiri.com, best site for learning double
Function find()
in Cplus Plus
its definition
The find() function searches the text that called it for the firstindexThere is, starting with him, the same text or letter that we pass to it in the place of the parameterstr
and it returns it.
In other words, we give it any text or letter, and it returns the number of the first digit that was found there.
Find () function Define it It searches in the text that called it for the first index. Starting with it, there is the same text or letter that we pass to it in place of the parameter str and it returns it. In other words, we give it any text or letter, and it returns to us the number of the first digit it found.
By default this function starts the search process fromtheindexnumber 0 to the lastindexIt is in the text that called it.
If the text to be searched is not found, the value of the constant is returnedstring::npos
.
Build the function find
size_t find (const string& str, size_t pos = 0) const
size_t find (const string& str, size_t pos = 0) const noexcept
Parameters of the find .() function
The location of the parameter We
str
pass the text that the function will search for in the text that called it.Parameter
pos
is an optional parameter, we put a number in its place in case we want to make the function start the search process from a numberindexspecific and not fromtheindexnumber0 .
return value of the find() function
Returns a number representing the firstindexIt has the text or character that we passed to it, and the value of the constant is returned string::npos
if no match is found.
First example of the find .() function
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "I have one cat. My cat like milk."; // thus it will print 11 'c' then there is the text str here we have printed the number of the first digit of the variable cout << str.find("c") << endl; // So it will print 11 'cat' then there is the text str Here we have printed the number of the first digits of the variable cout << str.find("cat") << endl; // Noting that we started the search process from field No. 14. Thus, 19 'cat' will be printed, then the text str is here. We have printed the number of the first cell in the variable cout << str.find("cat", 14) << endl; // string::npos and since there is no such word, the value of the constant will be printed. 'dog' then there is the text str. Here we print the number of the first digit of the variable cout << str.find("dog") << endl; return 0; }
• We will get the following result when running.
11 11 19 18446744073709551615
In actual programs, the method of checking to see if the value has been found may be more appropriate for you.
The second example is in the find .() function
#include <iostream> using namespace std; int main() { // keyword and the word we will search for in the variable str Here we have defined the body of the variable string str = "I have one cat. My cat like milk."; string keyword = "cat"; // result and store the search result in the variable keyword in the variable "cat" for the word str here we searched in the text in the variable size_t result = str.find(keyword); // string::npos with the value of result here we have compared the value of // If they are not equal, it means that the word we searched for has been found and the number of the first cell in which the word was found will be printed. if (result != string::npos) { cout << "'" << keyword << "' is found at index " << result; } // If they are equal, then the word we searched for was not found and it will print that it was not found else { cout << "'" << keyword << "' is not found!"; } return 0; }
• We will get the following result when running.
'cat' is found at index 11
Function rfind()
in Cplus Plus
its definition
The rfind() function searches the text that called it for anotherindexThere is starting from him the same text or character that we pass to it in the place of the parameter str
and it returns it.
In other words, we give it any text or letter, and it returns to us the number of the last digit it was found at.
Rfind () function Define it It searches in the text that called it for the last index. Starting with it, there is the same text or letter that we pass to it in place of the parameter str and it returns it. In other words, we give it any text or letter, and it returns to us the number of the last digit it found.
By default this function starts the search process fromtheindexThe last in the text that called it totheindexnumber0 .
If the text to be searched is not found, the value of the constant is returnedstring::npos
.
Build the function rfind
size_t rfind (const string& str, size_t pos = 0) const
size_t rfind (const string& str, size_t pos = 0) const noexcept
Parameters of the rfind() function
The location of the parameter We
str
pass the text that the function will search for in the text that called it.Parameter
pos
is an optional parameter, we put a number in its place in case we want to make the function stop the search process before reaching a numberindexspecific.
The return value of the rfind() function
Returns a number representing anotherindexIt has the text or character that we passed to it, and the value of the constant is returned string::npos
if no match is found.
First example of the rfind() function
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "I have one cat. My cat like milk."; // thus it will print 19 'c' then there is the text str here we have printed the number of the last digit of the variable cout << str.rfind("c") << endl; // So 19 'cat' will be printed, then there is the text str. Here we have printed the number of the last digit of the variable cout << str.rfind("cat") << endl; // Noting that we stopped the search process at field number 14. Thus 11 'cat' will be printed, then the text str is here. We have printed the number of the last cell in the variable cout << str.rfind("cat", 14) << endl; // string::npos Since there is no such word, the value of the constant will be printed. 'dog' then there is the text str. Here we print the number of the last digit of the variable cout << str.rfind("dog") << endl; return 0; }
• We will get the following result when running.
19 19 11 18446744073709551615
In actual programs, the method of checking to see if the value has been found may be more appropriate for you.
The second example is in the rfind() function
#include <iostream> using namespace std; int main() { // keyword and the word we will search for in the variable str Here we have defined the body of the variable string str = "I have one cat. My cat like milk."; string keyword = "cat"; // result and store the search result in the variable keyword in the variable "cat" for the word str here we searched in the text in the variable size_t result = str.rfind(keyword); // string::npos with the value of result here we have compared the value of // If they are not equal, it means that the word we searched for has been found and the number of the last cell at which the word was found will be printed. if (result != string::npos) { cout << "'" << keyword << "' is found at index " << result; } // If they are equal, then the word we searched for was not found and it will print that it was not found else { cout << "'" << keyword << "' is not found!"; } return 0; }
• We will get the following result when running.
'cat' is found at index 19
replace()
C++
function
its definition
The replace() function is used to modify a specific part of the text that called it.
The replace () function Define it Used to modify a specific part of the text that called it.construct() replace . function
string& replace (size_t pos, size_t len, const string& replacement);
Parameters of the replace .() function
Parameter location We
pos
passindex
the character from which we want to start changing the text.Parameter location We
len
pass a number representing how many characters you want to change as of theindex
parameterpos
.The location of the parameter We
replacement
pass the text to be added in place of the part to be deleted.
return value of the replace() function
Returns the resulting text from the switch operation.
Example in the replace .() function
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "welcome to youtube.com"; // "alawiri" number 11 in the text index starting from the character in the str here we have replaced the first 7 characters in the text str.replace(11, 7, "alawiri");
// after we made a modification to it str here we print the value of the variable cout << str; return 0; }
• We will get the following result when running.
welcome to alawiri.com
swap()
C++
function
its definition
The swap() function replaces the content of the text that called it with the content of the text we pass in place of the parameterstr
.
It also replaces the content of the variable we pass in place of the parameter str
with the content of the text that called it.
Build the function swap
void swap(string& str)
Parameters of the function swap .()
In place of the parameter
str
we pass the text variable with which the content will be exchanged.
Return value of the function swap .()
does not return a value.
An example of using the swap() function
#include <iostream> using namespace std; int main() { // s2 and another text variable named s1 here we have defined a text variable named string s1 = "alawiri";
string s2 = "youtube"; // s2 and s1 here we have swapped the value of the two variables s1.swap(s2); // After we switched their values s2 and s1 here we printed a value cout << "s1 = " << s1 << endl; cout << "s2 = " << s2 << endl; return 0; }
• We will get the following result when running.
s1 = youtube s2 = alawiri
append()
C++
function
its definition
The append() function adds the value of the text we pass in place of the parameter str
at the end of the invoked text and returns the resulting text when combined.
build the append() function
string& append (const string& str)
Parameters of the append() function
In place of the parameter
s
we pass the text that will be added at the end of the text that called the function.
Return value of the append() function
Returns a new text containing the value of the text that called the function plus the value of the text we pass to the function.
An example of using the append() function
#include <iostream> using namespace std; int main() { // Here we have defined two variables containing texts string s1 = "This part is from s1. "; string s2 = "This part is from s2"; // s1 at the end of the variable s2 the value of the append() variable will add the function // s3 After that, the answer to be returned will be stored in string s3 = s1.append(s2); // s3 Here we have printed the value of the variable cout << s3; return 0; }
• We will get the following result when running.
This part is from s1. This part is from s2
You may be wondering what is the difference between using a function append()
and writing the following.
string s3 = s1 + s2;
The only difference is that the function append()
executes faster.
empty()
C++
function
its definition
The empty() function is used to check if the text that called it is empty.
Empty () function Define it Used to check whether the text that called it is blank or not.construct the empty .() function
bool empty() const
bool empty() const noexcept
Parameters of the function empty .()
Do not accept any parameters.
Return value of the function empty .()
Returns 1
- which means true
- if the script that called it is not empty (ie, contains at least one character).
And return 0
- which means false
- if not.
An example of using the empty .() function
#include <iostream> using namespace std; int main() { // does not contain any text s here we have defined a variable named string s; // empty s contains a value or not. And 0 will be printed because s here we have printed whether the variable is cout << s.empty() << endl; // s Here we put a text in the variable s = "I like cats"; // not empty s contains a value or not. And 1 will be printed because s here we have printed if the variable is cout << s.empty(); return 0; }
• We will get the following result when running.
0 1
clear()
C++
function
its definition
The clear() function clears all characters in the calling text.
Clear () function Define it Clears all characters in the text that called it.build() function clear
bool clear() const
bool clear() const noexcept
Parameters of the function clear .()
Do not accept any parameters.
return value of the function clear
does not return a value.
An example of using the clear() function
#include <iostream> using namespace std; int main() { // contains text s here we have defined a variable named string s = "I like cats"; // s Here we have cleared the text in the variable s.clear(); // and of course nothing will be printed because it is empty s here we have printed the content of the variable cout << s; return 0; }
• We will get the following result when running.
compare()
C++
function
its definition
The compare() function compares the size of the text it was called to with the size of the text we pass to it in the parameter's placestr
.
It returns a number representing the size difference between them.
Build the compare() function
There are two forms of this function. It can be called in two different ways.
int compare (const string& str) const
int compare (const string& str) const noexcept
int compare (const string& str) const noexcept
Parameters of the compare .() function
In place of the parameter str
we pass the text that will be compared with the text that called the function.
Return value of the compare() function
Returns
0
if they are the same size.Returns
1
if the recalled text has more characters.Returns
-1
if the recalled script has a smaller number of characters.
Example of using the compare .() function
#include <iostream> using namespace std; int main() { string s1 = "I like cats."; string s2 = "I'm learning c++ from alawiri.com"; string s3 = s1; cout << s1.compare(s2) << endl; // s2 is less than the number of characters s1 here will print 1- because the number of characters cout << s2.compare(s1) << endl; // s1 is greater than the number of characters s2 here will print 1 because the number of characters cout << s1.compare(s3) << endl; // s3 equals the number of characters s1 here it will print 0 because the number of characters return 0; }
• We will get the following result when running.
-1 1 0
insert()
C++
function
its definition
The insert() function adds the value of the text we pass in place of the parameterstr
in the text that called it whenIndexThe cell we specify in place of the parameter pos
and returns the resulting text when merging them together.
Build the insert() function
string& insert (size_t pos, const string& str)
Parameters of the insert() function
In place of the parameter
str
we pass the text to be added in the text that called the function.Parameter location
pos
we pass a numbertheindex
where we want to add the text.
The return value of the insert() function
Returns a new text containing the value of the text that called the function plus the value of the text we pass to the function.
An example of using the insert() function
#include <iostream> using namespace std; int main() { // str Here we have defined a text variable named string str = "welcome alawiri.com"; // str number 8 in the index text at the "to" here we have added the word str.insert(8, "to "); // after we made a modification to it str here we print the value of the variable cout << str; return 0; }
• We will get the following result when running.
welcome to alawiri.com