Min menu

Pages

VB

String functions in C++

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 poswe pass indexthe character from which we want to start transcribing the text.
Parameter location lenYou can pass a number representing how many characters you want to copy from the indexparameter posif 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 strand 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 strand 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 pospass indexthe character from which we want to start changing the text.
Parameter location We lenpass a number representing how many characters you want to change as of the indexparameterpos.
The location of the parameter We replacementpass 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 strwith 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 strat the end of the text that called it and returns the resulting text when combined.
append() function
bool empty() Returns trueif the invoking text is not empty (ie, contains at least one character).
And return falseif 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 0if they are the same size.
Returns 1if 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 posand 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.

Length () function Define it Returns a number that represents the number of characters in the text that called it. For accuracy, the number you return represents how much byte the text consists of.

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

Main.cpp
                    #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 .()

Main.cpp
                    #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.

Size () function Define it Returns a number that represents the number of characters in the text that called it. For accuracy, the number you return represents how much byte the text consists of.

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() 

Main.cpp
                    #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() 

Main.cpp
                    #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 poswe pass indexthe character from which we want to start transcribing the text.

  • Parameter location lenYou can pass a number representing how many characters you want to copy from the indexparameter posif you don't want to copy all the existing text.



Return value of the substring() function

come backString ansubstrFromtheStringwho 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 posis. The following error will appear when you run the program.



Example of the function substring()

Main.cpp
                    #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 strpass the text that the function will search for in the text that called it.

  • Parameterpos 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::nposif no match is found.



First example of the find .() function

Main.cpp
                    #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

Main.cpp
                    #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 strand 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 strpass the text that the function will search for in the text that called it.

  • Parameterpos 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::nposif no match is found.



First example of the rfind() function

Main.cpp
                    #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

Main.cpp
                    #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 pospass indexthe character from which we want to start changing the text.

  • Parameter location We lenpass a number representing how many characters you want to change as of the indexparameterpos.

  • The location of the parameter We replacementpass 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

Main.cpp
                    #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 strwith the content of the text that called it.

Swap () function Define it The content of the text that called it is replaced by the content of the text that we pass it in place of the str parameter. It also replaces the content of the variable we pass in place of the str parameter 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 strwe 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

Main.cpp
                    #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 strat the end of the invoked text and returns the resulting text when combined.

Append () function Define it It adds the value of the text we pass in place of the str parameter at the end of the text that called it and returns the resulting text when combined together.

build the append() function

                  string& append (const string& str)
  


Parameters of the append() function

  • In place of the parameter swe 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

Main.cpp
                    #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

Main.cpp
                    #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

Main.cpp
                    #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.

Compare () function Define it Compare the size of the text that called it with the size of the text that we pass it to in place of the parameter str. And returns a number that represents 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 strwe pass the text that will be compared with the text that called the function.



Return value of the compare() function

  • Returns 0if they are the same size.

  • Returns 1if the recalled text has more characters.

  • Returns -1if the recalled script has a smaller number of characters.



Example of using the compare .() function

Main.cpp
                    #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 posand returns the resulting text when merging them together.

Insert function () Define it The value of the text that we pass adds the parameter location at the index that called it at the index to the place we specify the place of the parameter pos and returns the text when combined together.

Build the insert() function

                  string& insert (size_t pos, const string& str)
  


Parameters of the insert() function

  • In place of the parameter strwe pass the text to be added in the text that called the function.

  • Parameter locationpos we pass a numbertheindexwhere 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

Main.cpp
                    #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
___________